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.
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:
A bool
true
value is converted to the string"1"
. boolfalse
is converted to""
(the empty string). This allows conversion back and forth between bool and string values.
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:
$falsevar = false;
$truevar = true;
echo "Variable set to false is [" . $falsevar . "]\n";
echo "Variable set to true is [" . $truevar. "]\n";
As described in the manual, this results in:
Variable set to false is []
Variable set to true is [1]
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:
$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;
}
}
var_dump($wantsItem);
That gives the output:
bool(false)
Aside, as Robert Mihai Ionas pointed out, you have a subtle bug in your code, unrelated to the question: strpos
returns 0
for "matches at the start", and false
for "does not match", and this causes a different type-juggling problem: both are "false-y values". So as the strpos
manual page notes, you should use strpos($prompt, $needle) !== false
instead of strpos($prompt, $needle) != false
.
Alternatively, you can use the recently-added str_contains
function instead, which doesn't have this fiddly problem.