Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How construct `print` works?

Tags:

php

I'm currently browsing issues for Zend PHP 5 Certification Practice Test and saw the following:

print print('5') + print('4');
// output
// 461

// or

echo '3' + '0.5' , print('2') + 1;
// output 
// 3.531

How does it work? Description and examples from http://php.net/manual/en/function.print.php did not clarify anything…

like image 858
voodoo417 Avatar asked Dec 30 '13 20:12

voodoo417


4 Answers

print is not a function, it's a language construct. It always returns 1, and its precedence is lower than arithmetic operators. So

print print('5') + print('4');

is equivalent to:

print (print ('5' + print '4'));

This executes the following steps:

  1. print 4 -- prints 4 and returns 1.
  2. Calculate 5 + 1, which returns 6.
  3. print 6 -- prints 6 and returns 1
  4. print 1 -- prints 1 (also returns 1, but there's nothing receiving the result so irrelevant).

So

echo '3' + '0.5' , print('2') + 1

is equivalent to:

echo (3 + 0.5);
echo (print (2 + 1));

This executes the following steps:

  1. Calculate 3 + 0.5, which returns 3.5
  2. echo 3.5, which prints 3.5
  3. Calculate 2 + 1, which returns 3
  4. print 3, which prints 3 and returns 1.
  5. echo 1, which prints 1
like image 86
Barmar Avatar answered Nov 10 '22 01:11

Barmar


print print('5') + print('4');

First it is printed/outputed 4 (right to left). Language construct print always returns 1 (outputs if echoed/printed), so print('5') + 1 will output 6. Then last print prints out return of construct print, which is 1 (as said before). So final output would be 461.


echo '3' + '0.5' , print('2') + 1;

In echo langauge construct, character , is concatenation. First '3' + '0.5' is outputed, which is 3.5. Then again, as before, print('2') + 1 will return 3, and when echoing print will output 1. So final output would be 3.531.

like image 2
Glavić Avatar answered Nov 10 '22 01:11

Glavić


Whether the '4' print fires first, or the '5' print does may be implementation dependent. In this case, the '4' printed first, and its "print" returned a 1 which appears to have been added to '5' to get '6'. Finally, the '1' return code for the print('5') was output.

'3' + '0.5' gives '3.5'. After that (comma sequencing point), '2' + 1 was printed ('3'), and finally the print returned '1' return code.

I'm not sure I'd regard any of this dependence on sequencing and order of execution as good programming practice. If you need it for an exam, I suspect it may be more of a "gotcha" or warning of what not to do...

'print' and 'echo' behave somewhat differently, but I don't have my PHP texts at hand to describe exactly how they are different.

like image 1
Phil Perry Avatar answered Nov 10 '22 02:11

Phil Perry


First of all, print always returns 1. And secondly it can be called without parenthesis. And lastly the operator precedence has to be taken into account.

With this your first statement can be dissected into the following arithmetic structure:

  1. Evaluate print('4'): output 4 and return 1
  2. Evaluate print('5') + 1: output 6 and return 1
  3. Evaluate print: output 1

The same works with your second statement, but here you must take the aformentioned operator precedence into account, + has a higher one than ,:

  1. Evaluate echo 3 + 0.5: output 3.5
  2. Evaluate print('2') + 1: output 3 and output the return value of print(), 1
like image 1
nietonfir Avatar answered Nov 10 '22 01:11

nietonfir