Strange echo, print behaviour in PHP?

The following code outputs 43211, why?

  echo print('3').'2'.print('4');

Solution 1:

Your statement parses to humans as follows.

Echo a concatenated string composed of:

  1. The result of the function print('3'), which will return true, which gets stringified to 1
  2. The string '2'
  3. The result of the function print('4'), which will return true, which gets stringified to 1

Now, the order of operations is really funny here, that can't end up with 43211 at all! Let's try a variant to figure out what's going wrong.

echo '1' . print('2') . '3' . print('4') . '5';

This yields 4523111

PHP is parsing that, then, as:

echo '1' . (print('2' . '3')) . (print('4' . '5'));

Bingo! The print on the left get evaluated first, printing '45', which leaves us

echo '1' . (print('2' . '3')) . '1';

Then the left print gets evaluated, so we've now printed '4523', leaving us with

echo '1' . '1' . '1';

Success. 4523111.

Let's break down your statement of weirdness.

echo print('3') . '2' . print('4');

This will print the '4' first, leaving us with

echo print('3' . '2' . '1');

Then the next print statement is evaluated, which means we've now printed '4321', leaving us with

echo '1';

Thus, 43211.

I would highly suggest not echoing the result of a print, nor printing the results of an echo. Doing so is highly nonsensical to begin with.


Upon further review, I'm actually not entirely sure how PHP is parsing either of these bits of nonsense. I'm not going to think about it any further, it hurts my brain.

Solution 2:

Much of the confusion is due to the placement of parentheses around the arguments to print. As you know, parentheses are optional with language constructs; what you probably didn't know is that they're removed during parsing.

Evaluation order

Let's remove the parentheses first:

echo print '3' . '2' . print '4';

And illustrate the actual order of evaluation:

echo (print ('3' . '2' . (print '4')))
^     ^      ^                     ^
3     2      1--------->>----------1

In the heart of this you will find a concatenation of strings or string representations; this is evaluated first:

'3' . '2' . (print '4')

The first two elements are concatenated:

'32' . (print '4')

Then, the value of (print '4') is evaluated; after printing its argument '4', the return value of print itself is always int(1); this is cast into a string '1' and concatenated with the other elements:

'321'

This concludes the first step. The second step passes the temporary results to another print statement:

print '321'

As before, '321' is printed and now int(1) is returned for the last step:

echo 1

Proof

You can confirm this behaviour when you look at the opcodes that are generated (output column is added for clarity):

line     # *  op          return  operands        output
------------------------------------------------+-------
   1     0  >   CONCAT      ~0      '3', '2'    |
         1      PRINT       ~1      '4'         | 4
         2      CONCAT      ~2      ~0, ~1      | 4
         3      PRINT       ~3      ~2          | 4321
         4      ECHO        ~3                  | 43211

Explanation

  • "3" and "2" are concatenated - "32" - and stored into ~0.
  • "4" is printed and the return value int(1) is stored into ~1.
  • ~0 and ~1 are concatenated - "321" - and stored into ~2.
  • "321" is printed and the return value is stored into ~3.
  • int(1) is printed as "1" due to string casting.

Solution 3:

print is returning 1

On the documentation

Return Values: Returns 1, always.

You should just probably stick to using echo.