[PHP] concatenation operator and addition operator

What will the following script output?

 

 

<?php
echo 'Testing ' . 1 + 2 . '45';
?>

 

 

"Testing 345"?

 

No, we can get the reason from the book of Zend PHP Certification Test Book".

 

This question tests your knowledge of string manipulation and operator precedence. The
concatenation operator has a higher precedence than the addition operator. Therefore, PHP
will interpret this expression as if it were written as ('Testing ' . 1) + (2 . '45'). When the
addition takes place, the first concatenation is evaluated as the integer zero, since the string
Testing 1 is not a valid number. The second concatenation is evaluated as the number 245
and, therefore, PHP outputs the result of 0 + 245, that is, 245. 

posted @ 2010-05-14 11:15  DavidHHuan  阅读(292)  评论(0编辑  收藏  举报