<?php if ($foo = $bar) … ?>

Posted Monday, May 15, 2006 at 15h06 in Computers

PHP sure has some weird conditional matches. I thought it was a general rule that "if ($foo = $bar)" always evaluated TRUE unless $bar was NULL. But apparently that's not the case in PHP:

Case 1:

  1. $bar = NULL;
  2. if ($foo = $bar)
  3.     echo 'TRUE';
  4. else
  5.     echo 'FALSE';
  6. // Result: FALSE

Case 2:

  1. $bar = FALSE;
  2. if ($foo = $bar)
  3.     echo 'TRUE';
  4. else
  5.     echo 'FALSE';
  6. // Result: FALSE

Case 3:

  1. $bar = 0;
  2. if ($foo = $bar)
  3.     echo 'TRUE';
  4. else
  5.     echo 'FALSE';
  6. // Result: FALSE

Case 4:

  1. $bar = (string) 0;
  2. if ($foo = $bar)
  3.     echo 'TRUE';
  4. else
  5.     echo 'FALSE';
  6. // Result: FALSE

Case 5:

  1. $bar = 1;
  2. if ($foo = $bar)
  3.     echo 'TRUE';
  4. else
  5.     echo 'FALSE';
  6. // Result: TRUE

I can understand case 1 and maybe case 2, but cases 3 and 4 were surprising to me. I wonder if it's the same in C, assuming the variables are already declared? It's been a long time since I've coded in C, but I don't remember it being this way.

5 Comments »

Comment from pekkle on May 16, 2006 at 6:09 am

The equality comparison operator in PHP is an ‘=’? What’s the assignment operator?

Or are you deliberately assigning $bar to $foo?

In C a boolean expression is False if it evaluates to 0; otherwise I believe it is treated as True (non-zero, usually 1 (I don’t remember if the programmer has to specify the non-zero value).

NULL is the value to which pointers are assigned to mean it has no memory assigned to it.

A string var has to be compared specifically to an empty string to test if it has any characters assigned to it.

In C++ I’m not sure how it all works …

In Perl, I think Null is a specific value meaning ‘undetermined’; 0 and FALSE are treated as FALSE; an empty string I think also evaluates to False. Casting the integer 0 to a string … I’m not sure. It looks like in PHP it returns an empty string and not a character value for the integer ‘0′.

Does an empty string evaluate to False in PHP?

Comment from stevec on May 16, 2006 at 9:20 am

= is the assignment operator. == is the comparison operator. Syntax is the same as C. Which is why I was confused, because I thought the assignment operator always evaluated TRUE unless you tried to assign something to NULL. Actually, now that I think about it, assigning something to NULL should also return TRUE in C, right?

Unlike C, in PHP you don’t have to declare variables. It’s a lot easier for the lazy coder that way. So in the above cases, $foo is never declared, and could be anything (char, int, boolean). In PHP if you assign, for example, an int ($foo) to a char/string ($bar), then $foo becomes a char/string.

For example, the following is completely valid:

$foo = (int) 0;
$bar = (string) ‘bar’;
$foo = bar;
echo $foo; // returns ‘bar’

Comment from stevec on May 16, 2006 at 9:33 am

BTW, the reason I bring this up is because I saw in someone’s script the use of something like,

if ($foo = db_query('SELECT field FROM table'))

as a shortcut for

if (db_query('SELECT field FROM table'))
   $foo = db_query('SELECT field FROM table');

where db_query is a function which returns NULL (or empty array?) if no values found. At first when I saw this I thought it was a typo (= instead of ==), but then I realized it wasn’t and, following up on it, discovered the weird results described in my post.

Comment from pekkle on May 17, 2006 at 1:34 am

Hmmm … I think the value NULL evaluates to False in C as well, unless you make an explicit comparison (e.g. if (i == NULL) { … } ) and the resultant value is the value of the expression (if i is a pointer variable and had been assigned to null prior to the comparison, the comparison should evaluate to True). But if the comparison is simply if (i) { … } with the same conditions, it would evaluate to False, I believe.

Comment from stevec on May 19, 2006 at 7:58 pm

Ya, that’s what I originally thought too. So NULL is one case where PHP and C are the same. But the “FALSE” and “0″ cases are just plain weird in PHP. :p

RSS feed for comments on this post | TrackBack URI

Leave a comment

XHTML: You can use these tags:

<a href="" title="">
<abbr title="">
<acronym title="">
<b>
<blockquote cite="">
<code>
<em>
<i>
<strike>
<strong>