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…
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:
print 4
-- prints 4
and returns 1
.5 + 1
, which returns 6
.print 6
-- prints 6
and returns 1
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:
3 + 0.5
, which returns 3.5
echo 3.5
, which prints 3.5
2 + 1
, which returns 3
print 3
, which prints 3
and returns 1
.echo 1
, which prints 1
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
.
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.
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:
print('4')
: output 4 and return 1print('5') + 1
: output 6 and return 1print
: output 1The same works with your second statement, but here you must take the aformentioned operator precedence into account, +
has a higher one than ,
:
echo 3 + 0.5
: output 3.5print('2') + 1
: output 3 and output the return value of print()
, 1If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With