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
Can someone explain to me 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.)
Thanks for any suggestions.
3
Answers
Use !== false because 0 is equal with false
What you are seeing is the result of "type juggling", where you have a variable which is a boolean, and you’re using it somewhere you need a string.
The rules for that are described in the "Strings" section of the manual:
Your first example is a bit confusing, because you haven’t offset the value with a newline or other separator, and your strings begin "falsevar" and "truevar", making it really hard to read the output. Here’s a clearer version:
As described in the manual, this results in:
For debugging, it’s generally more useful to use a function like
var_dump
, which gives you a description of a value. In your second example, you could write this:That gives the output:
Aside, as Robert Mihai Ionas pointed out, you have a subtle bug in your code, unrelated to the question:
strpos
returns0
for "matches at the start", andfalse
for "does not match", and this causes a different type-juggling problem: both are "false-y values". So as thestrpos
manual page notes, you should usestrpos($prompt, $needle) !== false
instead ofstrpos($prompt, $needle) != false
.Alternatively, you can use the recently-added
str_contains
function instead, which doesn’t have this fiddly problem.It doesn’t.
false
is echoed as an empty string.truevar
in your output is the beginning of the second string that you echo, not the value of$falsevar
.it would have been clearer if you put the outputs on separate lines.
Then the output would have been: