If and empty strangeness

When I write

$char='a' if (!empty($char)){ echo 'true'; } else { echo 'false'; } 

I believe, but when:

 if ($char='a' && !empty($char)){ echo 'true'; } else { echo 'false'; } 

Why am I getting a lie?

+4
source share
3 answers

Because the second way is the same as:

 if ($char = ('a' && !empty($char))){ echo 'true'; } else { echo 'false'; } 

&& has higher priority than = , so $char will be false .

+2
source

because you write if($char = 'a' && !empty($char)) , you use the assignment operator, use == , u should get the correct result.

0
source

Because of the single, there is one equal sign.

 if ($char='a' 

really

 if ($char=='a' 
0
source

All Articles