I would like to use a true/false result to do alternative things in PHP. I was surprised by the following:
$falsevar = false;
$truevar = true;
echo "falsevar which I just set to false is".$falsevar; //displays true
echo "truevar which I just set to true is".$truevar; //displays 1
Output:
falsevar which I just set to false istruevar which I just set to true is1
What should I make of this result? Why would PHP echo out false as true?
Further confusing me, my use case is the following:
$prompt = "I would like a sandwich to eat";
$myarray = array("pizza", "hamburger", "hot dog");
$wantsItem = false;
foreach ($myarray as $needle) {
if (strpos($prompt, $needle) != false) {
echo "Match found: {$needle}\n";
$wantsItem = true;
}
}
echo "wants item is".$wantsItem;
When I run this this which I believe should return false, nothing echoes at all.
falsevar which I just set to false istruevar which I just set to true is1value of wants item
What is the behaviour of true and false in PHP? (Note, I understand that true and false are case insensitive so that should not be the issue.)
$falsevaris not outputtingtrue, it's outputting nothing, then the next echo is outputting the string "truevar". Adding a linebreak between your two echos might make it make more sense