I have an array. Inside that array my Fruit_Name could either be Pear or Apple depending on the button i select in the previous page. Lets say i select Apple the if statement does not seem to work however it does echo. It echos$FruitType
and it seems to get Apple which should fire up my if statement and show me "You did it!", but my if condition doesn't. What am i doing wrong?
Array
Fruit_Name=Apple
My Function
function GetField($arr, $field)
{
$result = ' ';
foreach($arr as $line)
{
if (explode('=', $line) [0] == $field)
{
$result = explode('=', $line) [1];
}
}
return $result;
}
$FruitType= GetField($array, 'Fruit_Name');
echo $FruitType;
if ($FruitType == "Apple")
{
echo "You did it!";
}
else if ($FruitType == "Pear")
{
echo "Its not Pear!";
}
When comparing strings (and really, you should do this with any data type) in php, you must use the === notation. The == is only used for numbers, and when used on strings leads to problems like the one you're facing.
So your code should be
If ( $FruitType === "Apple" )
And the Getfield function is incorrect, as mentioned and explained by Dagon.