Forum Moderators: coopster
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 :)
$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;
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>";
}
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...
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....
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.