Strange echo, print behavior in PHP?

The following code displays 43211why?

  echo print('3').'2'.print('4');
+3
source share
4 answers

Your expression analyzes people as follows.

An echo concatenated string consisting of:

  • The result of a function print('3')that returns true, which will be contracted to1
  • String '2'
  • The result of a function print('4')that returns true, which will be contracted to1

Now the order of operations here is really funny, and it just can't end with 43211! Try the option to find out what is going wrong.

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

This gives 4523111

PHP parses this and then:

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

Bingo! First printleft evaluated, typing '45', which leaves us

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

print, '4523',

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

. 4523111.

.

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

'4',

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

, , '4321',

echo '1';

, 43211.

echo print, print echo. .


, , PHP . , .

+14

print. , ; , , , , .

:

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

:

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

; :

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

:

'32' . (print '4')

(print '4'); '4', print int(1); '1' :

'321'

. print:

print '321'

, '321' , ttc int(1):

echo 1

Proof

, , ( ):

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

  • "3" "2" - "32" - ~0.
  • "4" , int(1) ~1.
  • ~0 ~1 - "321" - ~2.
  • "321" , ~3.
  • int(1) "1" - .
+3

print 1

: 1, .

, echo.

+2

, alex. .

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

. ​​

0

Source: https://habr.com/ru/post/1756664/


All Articles