-4

Can you set a variable in php to false?

Can this code be correct:

if($jack = "Not tall"){

$jack = FALSE;

}
if(!jack){do whatever;}
Tim Penner
  • 3,551
  • 21
  • 36
Dre_Dre
  • 745
  • 2
  • 6
  • 15

2 Answers2

3

When you want to compare values, make sure to have == for "is equal to", or != "is not equal to". When you have one = then you are setting the variable and will always evaluate as true. PHP Comparison Operators

if($jack == "Not tall"){
  $jack = FALSE;
}
if(!$jack){
  // do whatever ...
}

And when dealing with variables, always make sure you have the $ in the front. This is very basic php stuff.

S.A
  • 568
  • 2
  • 13
0

setting a value in if statement always return true because it will return current value.

echo ($jack = "Not tall")

it will print "Not tall" so it will return true

$jack = FALSE;

it will always set false.

if($jack == "Not tall")
  $jack = FALSE;
if(!jack){do whatever;}

Differences between the Equality operator (==) and the Assignment operator (=)

Prashant Srivastav
  • 1,723
  • 17
  • 28