Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The if statement doesn't work for false in php

When trying to get familiar with if statement in PHP, this happened. First time i tried this code below.

if(true) {echo 'true';} else {echo 'false';}

And the output was true when the condition is true. Again, when the condition is false (if(false)) it echos false.

But i tried the same, using a variable as the condition, while changing the value of the variable.

$con='false';
if($con){echo 'true';} else{echo 'false';} 

At this situation the output is true even when the variable value is false or true. At the same time, the if statement working fine when 1 and 0 is used instead true and false. Why is this happening?

like image 904
DScript Avatar asked Jun 23 '15 06:06

DScript


3 Answers

$con='false';

That 'false' is a valid string which is not Boolean FALSE, just like $con='hi'; isn't.

$con=FALSE;  //this is false now

To quote from the Manual

To specify a boolean literal, use the constants TRUE or FALSE. Both are case-insensitive.

Also read these other options that you have

When converting to boolean, the following values are considered FALSE:

◦ the boolean FALSE itself
◦ the integer 0 (zero)
◦ the float 0.0 (zero)
◦ the empty string, and the string "0"
◦ an array with zero elements
◦ an object with zero member variables (PHP 4 only)
◦ the special type NULL (including unset variables)
◦ SimpleXML objects created from empty tags

Then you observed this

At the same time, the if statement working fine when 1 and 0 is used instead true and false. Why is this happening?

This is because 0 in any form is FALSE, either string or numeric. But the text false as a string is not false for reasons mentioned above.

Also read about PHP Strict Comparisons since you're learning, because

var_dump(0==FALSE);    //bool(true)
var_dump(0===FALSE);   //bool(false)   :)
like image 81
Hanky Panky Avatar answered Sep 17 '22 05:09

Hanky Panky


PHP does some sneaky things in the if expression. The following values are considered FALSE:

  • the boolean FALSE itself
  • the integer 0 (zero)
  • the float 0.0 (zero)
  • the empty string, and the string "0"
  • an array with zero elements
  • an object with zero member variables (PHP 4 only)
  • the special type NULL (including unset variables)
  • SimpleXML objects created from empty tags

Every other value is considered TRUE (including any resource).

You're actually passing a string that says the word false, rather than the value false itself. Because that isn't in the above list, it is actually considered true!

like image 32
Luke Avatar answered Sep 21 '22 05:09

Luke


As described in the section about expressions, expression is evaluated to its Boolean value. If expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE - it'll ignore it. More information about what values evaluate to FALSE can be found in the 'Converting to boolean' section.

So as per docs try using

$con = false;//boolean and not a string
if($con){echo 'true';} else{echo 'false';} 

When converting to boolean, the following values are considered FALSE:

◦ the boolean FALSE itself
◦ the integer 0 (zero)
◦ the float 0.0 (zero)
◦ the empty string, and the string "0"
◦ an array with zero elements
◦ an object with zero member variables (PHP 4 only)
◦ the special type NULL (including unset variables)
◦ SimpleXML objects created from empty tags

Check Docs (IF MANUAL)

like image 24
Narendrasingh Sisodia Avatar answered Sep 19 '22 05:09

Narendrasingh Sisodia