Forum Moderators: coopster

Message Too Old, No Replies

Variable/string issue

         

Lived

11:13 am on Mar 15, 2009 (gmt 0)

10+ Year Member



$Name = implode (' ¦¦ ', $array);
echo $Name;

This code will output :

something1 ¦¦ something2 ¦¦ something3 ¦¦ somthing4

and if i use $Name in this code nothing happen:

switch($something) :
case ($Name):
echo 'You got it!';
endswitch;

But if i use this code it works perfect:

switch($something) :
case (something1 ¦¦ something2 ¦¦ something3 ¦¦ somthing4):
echo 'You got it!';
endswitch;

I hope somebody understand me and my problem :)

sonjay

11:40 am on Mar 15, 2009 (gmt 0)

10+ Year Member



Where is the value of $something being set?

Lived

11:54 am on Mar 15, 2009 (gmt 0)

10+ Year Member



while($row = mysql_fetch_assoc($getName)){
$array[] = $row['something'] ;
}

penders

1:05 pm on Mar 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



case (something1 ¦¦ something2 ¦¦ something3 ¦¦ somthing4):

Presumably you mean... ?

case ("something1 ¦¦ something2 ¦¦ something3 ¦¦ somthing4"):

Presumably $Name is still in scope at the point you test it?

Lived

2:05 pm on Mar 15, 2009 (gmt 0)

10+ Year Member



while($row = mysql_fetch_assoc($getName)){
$array[] = $row['something'] ;
}

$Name = implode (' ¦¦ ', $array);
echo $Name;

switch($something) :
case ($Name):
echo 'You got it!';
endswitch;

will output nothing! But if i use the string directly in the code it works....

switch($something) :
case (something1 ¦¦ something2 ¦¦ something3 ¦¦ somthing4):
echo 'You got it!';
endswitch;

penders

4:28 pm on Mar 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



(Note: This forum converts the pipe character into a split vertical bar character - just in case you try copying code back from this forum!)

case (something1 ¦¦ something2 ¦¦ something3 ¦¦ somthing4):

This line not valid PHP - you'll get a (fatal) parse error (which I assume you are not getting?!) - you'll need to surround your string value in quotes (either single or double), which I guess you must already be doing? Anyway...

Both your code snippets should give the same result - they should both be successful! Either testing the value of $Name or using the value directly should not matter. So, there must be something different between the values of $something and $Name when they are compared.

What do you get if you try this:

if ($Name == $something) { 
echo "<p>You got it!</p>";
} else {
echo "<p>Oh no you didn't!<br>";
echo "Name = $Name<br>";
echo "something = $something</p>";
}

Lived

7:28 pm on Mar 15, 2009 (gmt 0)

10+ Year Member



If i test your code it fail and give:

Oh no you didn't it!
Name = something1
something = 'something1' ¦¦ 'something2' ¦¦ 'somthing3' ¦¦ 'somthing4'

Pretty wierd and i have no clue why it don't work!

Lived

7:45 pm on Mar 15, 2009 (gmt 0)

10+ Year Member



$Name == 'something1' ¦¦ 'something2' ¦¦ 'somthing3' ¦¦ 'somthing4'

will output:

You got it!

penders

11:42 pm on Mar 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ok, so now you have single quotes around your somethingN values which you didn't have in the beginning - but seems to be correct?!

Also, in the beginning, you state:

echo $Name;
This code will output :
something1 ¦¦ something2 ¦¦ something3 ¦¦ somthing4

But in the code above $Name outputs:

something1

Something is obviously happening between your echo and switch statements? Is this all in the same code block? Is $Name in scope (my original question)?

$array[] = $row['something'] ;

Is this from your actual code? It may not throw an error, but using the variable $array as your array variable is bad programming practice and could lead to confusion with PHP's built in language constructs.

As sonjay asked above, "Where is the value of $something being set?"

You could post a more complete code example...

Lived

5:43 am on Mar 16, 2009 (gmt 0)

10+ Year Member



Here is the complete code i want to use:

while($row = mysql_fetch_assoc($getName)){
$array[] = $row['something'] ;
}

$Name = implode (' ¦¦ ', $array);

switch($something) :
case ($Name): //here is where the string goes
echo 'You got it!';
endswitch;

What i want is to pick some values from an database and use in a case statment....

sonjay

12:17 pm on Mar 16, 2009 (gmt 0)

10+ Year Member



So where is $something being set? Try this and see if $something and $Name are what you expect them to be:

while($row = mysql_fetch_assoc($getName)){
$array[] = $row['something'] ;
}
$Name = implode (' ¦¦ ', $array);
echo "something: $something<br>";
echo "Name: $Name";

Lived

4:20 pm on Mar 16, 2009 (gmt 0)

10+ Year Member



When i echo the string it's look like it should, but it don't work as a string when i try to use it in the code.

echo $string; OK

if{something == $string} FAIL

Lived

5:53 pm on Mar 16, 2009 (gmt 0)

10+ Year Member



My misstake did cause my problem....
case ($Name):
should be
case ($Name = $something):

Anyway thanx for your time.....

penders

7:53 pm on Mar 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



case ($Name = $something):

This is not correct. This assigns (single equals) $something to $Name and so will always evaluate to true (giving you the result you are expecting).

If anything you should being using the comparison operator (double equals):

case ($Name == $something):

But this is the same as you original code:

switch ($something): 
case ($Name): // etc.

penders

9:57 pm on Mar 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This assigns (single equals) $something to $Name and so will always evaluate to true...

(...providing $something evaluates to true)