skip to Main Content

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


  1. Use !== false because 0 is equal with false

    Login or Signup to reply.
  2. 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". bool false 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.

    Login or Signup to reply.
  3. Why would PHP echo out false as true?

    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.

    echo "falsevar which I just set to false is".$falsevar."<br>";
    echo "truevar which I just set to true is".$truevar."<br>";
    

    Then the output would have been:

    falsevar which I just set to false is
    truevar which I just set to true is1
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search