PHP Variable Assignment Within If Statement

Sunday, June 7, 2009 - 11:43

The usual practice when checking for the return value of functions is to run the function and store the value in a variable, and then test that variable. Here is an example of that process using the strstr() function.

1
2
3
4
5
6
7
$string = 'abcde';
$var = strstr($string, 'f');
if ( false !==  $var ) {
    var_dump($var);
} else {
    var_dump($var);
}

This code will output "bool(false)" as that was the return value of the strstr() function.

There is another way to write the same code within a single if statement. The following example assigns the variable and checks the return value in a single line of code.

1
2
3
4
5
6
7
$string = 'abcde';
 
if ( false !== $var = strstr($string, 'f') ) {
    var_dump($var);
} else {
    var_dump($var);
}

This code will output "bool(false)" again as this has the same function as the previous example.

There is one problem with assigning variables in this way, and this is that you can only do one. If you try to assign more than one variable at a time you will find that everything but the final variable will turn out to be the outcome of the boolean comparison. Take the following example, which uses the same $string variable as the other examples.

1
2
3
4
5
if ( false !== $var1 = strstr($string, 'a') 
    && false !== $var2 = strstr($string, 'b') 
    && false !== $var3 = strstr($string, 'c') ) {
    var_dump($var1, $var2, $var3);
}

The output of this code will be as follows:

bool(true) bool(true) string(3) "cde"

The first two comparisons come out as true and so $var1 and $var2 are assigned boolean values of true. The final comparison is the only one that is assigned the expected value. This might be a bug in PHP, but as this is a non-standard way of assigning variables I don't think many people have come across it.

Category: 
philipnorton42's picture

Philip Norton

Phil is the founder and administrator of #! code and is an IT professional working in the North West of the UK.
Google+ | Twitter

Comments

Great blog, reading it through RSS feed as well
I dare say the last example is correct behaviour. This is what variables are actually assigned.
1
2
... $var1 = (strstr($string, 'a') && false !== $var2 = strstr($string, 'b') && false !== $var3 = strstr($string, 'c'))
... $var2 = (strstr($string, 'b') && false !== $var3 = strstr($string, 'c'))
to get expected behaviour all one needs is enclosing assignments within ().
1
2
3
4
5
if ( false !== ($var1 = strstr($string, 'a'))
    && false !== ($var2 = strstr($string, 'b'))
    && false !== ($var3 = strstr($string, 'c'))) {
    var_dump($var1, $var2, $var3);
}
also by doing that - in theory - you're helping compiler (for tiny speedup) so it doesn't need to find out operator precedence, just eval from insidemost statement(s).
I had done this in the past successfully but have since forgotten the exact syntax. Thanks for this, it helped me tremendously.

Thanks guys!!

philipnorton42's picture
Submitted by philipnorton42 on Tue, 03/20/2018 - 16:19

@Jesus Yes. I completely agree. I always split out the assignment operators from if statements. To be honest I'm not sure where I got this code orignally, but I think I found it in someone elses code that I had to alter at the time. Trying to figure out that mess at the time was a real pain, but I came across this strange behaviour and thought I would post about it.

This is absolutely terrible practice to use this in your code.

It is also against generally accepted PSR standards: https://www.php-fig.org/psr/psr-2/#23-lines
as it is combining 2 statements (declaration statement and expression statement) into one line.

philipnorton42's picture
Submitted by philipnorton42 on Wed, 09/29/2010 - 09:56

Thanks for that Mirek, I did wander what was going on but kind of forgot about this article once I had published it. I really appreciate the input :)

Add new comment