Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP operator precedence & string concatination?

Tags:

php

See the following code snippet

$i=1;

echo $i.($i++);

in a quick, I thought the result would be 12 but the actual result is 21.

also

echo $i,$i++;

I thought it would be 12 but its 11 .

echo ($i = ($i++)); //result is 1

echo ($i = ($i+1)); //result is 2

But why?

like image 809
Red Avatar asked Oct 05 '13 15:10

Red


3 Answers

When a variable is not involved into any arithmetic operation (like your first $i), PHP will not create a temporary variable. Thus, your first $i will be evaluated at the end of the statement, when $i++ has already been executed.

To prevent this, you can still write:

echo ($i += 0).($i++);

But this is obviously not a good coding practice.

EDIT: When you use , it is actually syntactic sugar to shorten two PHP statements. It is strictly equivalent to:

echo $i;
echo $i++;

Since incrementation is executed after the last statement, 11 is indeed the result.

like image 78
Guillaume Poussel Avatar answered Nov 12 '22 07:11

Guillaume Poussel


First example

Code in brackets is evaluated first - in this case ($i++). The value of $i is taken (1) and then the variable is incremented to 2. So you then have this, where $i is 2.

echo $i . '1'

From this, the value of $i is substituted in, and you get '2' . '1', which is concatenated to give '21'.

Second example

It's easier to rewrite this to clear up the , separator. The line echo $i, $i++; is equivalent to:

echo $i;
echo $i++;

The first line obviously outputs 1, and the second will output that same value, then increment $i (++ is the post-increment operator). If you were to put another echo $i; at the end, it would output 2.

like image 24
George Brighton Avatar answered Nov 12 '22 08:11

George Brighton


As per the PHP documentation stated at: Operator Precedence

First Case

$i=1;

echo $i.($i++);

$i is initialized to value 1. Now, ++ follows a higer precedence than . and it has right-associative. This means your $i++ will be evaluated first. In this case , the value of $i++ will be 1 and the next value of $i will get incremented to 2. hence $i is 2

Now . has the next precendence after ++, which is left-associative. hence it will evaluate values starting from left.

so $i=2 and $i++ =1, hence the output 21

Second Case

$i=1;

echo $i,$i++;

Here, there is only one operator ++. Hence the need for comparision of precedence doesn't arise. Hence, it will be evalauted by default standard of left-associative. $i = 1, $i++ = 1. Hence 11

Third Case

echo ($i = ($i++)); //result is 1

In this case, now = is an assignment operator and is right-associative, so $i++ = 1. And since it is an assignment operator value of $i++ will be stored in $i. hence echo ($i = 1); which will result in output being 1.

Fourth Case

echo ($i = ($i+1)); //result is 2

Again, this will be right-associative, so $i+1 = 2. hence echo ($i = 2); which will result in output being 2.

like image 1
aaron Avatar answered Nov 12 '22 06:11

aaron