The if statement doesn't work for false in php

36

When trying to get familiar with{-code-1} statement in PHP, this happened. First time i tried this code below.

{-code-1}(true) {echo 'true';} else {echo 'false';}

And the output wastrue when the condition istrue. Again, when the condition isfalse ({-code-1}(false)) it echosfalse.

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

$con='false';
{-code-1}($con){echo 'true';} else{echo 'false';} 

At this situation the output istrue even when the variable value isfalse ortrue. At the same time, the{-code-1} statement working fine when1 and0 is used insteadtrue andfalse. Why is this happening?

718

Answer

Solution:

$con='false';

That'false' is a valid string which is not BooleanFALSE, 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 textfalse 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)   :)
495

Answer

Solution:

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!

389

Answer

Solution:

You are using 'false' as a STRING variable, but that is the WORD false, not the BOOLEAN constant. Just use false

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

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

And when you are doing if statements, this will work:

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

or you can use a === which compares one expression to another by value and by type.

if ($con === false) { echo 'false with type safe comparison!'; }
437

Answer

Solution:

Use a bool instead of a string:

$con = false;

Your if statement will simply check that $con is not empty, so in your example it will always be true.

862

Answer

Solution:

You can assign value to variable like this

$con=false;
192

Answer

Solution:

In your second example,$con isn't the booleanfalse, it's a string literal'false' (note the quotes), and any non-empty string in PHP evaluates astrue.

To fix this, just drop the quotes:

$con=false; // no quotes!
if($con){echo 'true';} else{echo 'false';} 
827

Answer

Solution:

Your $con declaration is a string not a bool. So it will always return true. To declare a boolean, use:

$con=FALSE;  //or TRUE
990

Answer

Solution:

'false' is not same asfalse.

if('true') orif('false') will resulttrue always as they will be treated asstrings and will be converted for comparison.

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

Will printfalse

238

Answer

Solution:

change the value of your variable to this

   $con = false;    

People are also looking for solutions to the problem: General php format

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.