Michiel
Michiel

Reputation: 8083

Combine an echo and a print in one statement

echo "1" . (print '2') + 3; returns 214. How does the script end up with *14?

Upvotes: 5

Views: 1982

Answers (5)

GravyCode
GravyCode

Reputation: 2531

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

You need to think of it in a logical order, of what happens first.

Before we can echo anything, the - "1" . (print '2') + 3 - we need to evaluate it to solve it.

First we write 1 down on a scrap of paper as the first part of our calculation.

Scrap paper: 1
Answer Sheet: 

We calculate "print '2'", which as a function writes the number 2 to our sheet of answer paper and returns a 1, which we write on our scrap piece of paper.

Scrap paper: 1 . 1 +3
Answer Sheet: 2

After this we want to concatenate the next piece on to the end, due to the "."

Scrap paper: 11 + 3 
Answer Sheet: 2

Now we put it together

Scrap paper: 11 + 3
Scrap paper: 14
Answer Sheet: 2

Then we echo out our scrap data to our answer sheet

Answer Sheet: 214

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

1.
Code--: echo "1" . (print '2') + 3;
Result: 
2.
Code--: echo "1" . 1 + 3;
Result: 2
3.
Code--: echo 11 + 3;
Result: 2
4.
Code--: echo 14;
Result: 2
5.
Code--: 
Result: 214

I hope that makes some sense, and remember the return of print is always 1, and any function that prints or echo's while inside another execution will echo/print before it's caller/parent does.

Upvotes: 3

Gordon
Gordon

Reputation: 316979

When you do

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

PHP will do (demo)

line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
   2     0  >   PRINT                                            ~0      '2'
         1      CONCAT                                           ~1      '1', ~0
         2      ADD                                              ~2      ~1, 3
         3      ECHO                                                     ~2
         4    > RETURN                                                   1

In words:

  • print 2, return 1
  • concat "1" with returned 1 => "11"
  • add "11" + 3 => 14
  • echo 14

and that's 214.

The operators + - . have equal Operator Precedence, but are left associative:

For operators of equal precedence, left associativity means that evaluation proceeds from left to right, and right associativity means the opposite.


Edit: since all the other answers claim PHP does 1+3, here is further proof that it doesnt:

echo "1" . (print '2') + 9;

gives 220, e.g. 11+9 and not 1 . (1+9). If the addition had precedence over the concatenation, it would have been 2110, but for that you'd had to write

echo "1" . ((print '2') + 9);

Upvotes: 14

ahmetunal
ahmetunal

Reputation: 3960

The 1 in between is actually a true statement. Because print statement actually returns a true. So you get 2 (from print), 1 (from echo print), and 4 (from 1+3)

Upvotes: 1

Jovan Perovic
Jovan Perovic

Reputation: 20191

print always return 1 according to: http://php.net/manual/en/function.print.php

So, because arithmetic operator has precedence over one for concatenation, so you get:

"1" . (1+3)

... that is "14" ;). And because print sends string directly to output you get '2' in front of everything....

Upvotes: -1

Alex Turpin
Alex Turpin

Reputation: 47776

print is executed first because of the parenthesis, so it print's 2 first, but it returns 1. Then, your echo gets executed, printing 1. You then concatenate to it the result of print (which is 1) with 3 added to it. That's 4.

Upvotes: -1

Related Questions