A line in PHP that doesn't make sense

I experimented with PHP's weak / dynamic typing properties in preparation for the test and was completely puzzled by the output of this string concatenation. Can someone please explain how this is possible?

<?php echo  1 . "/n" . '1' + 1 ?><br />

      

output:

2

+3


source to share


1 answer


Analysis:

echo  1 . "/n" . '1' + 1;

      

equivalent to

//joined first 3 items as string
echo "1/n1"+1;

      



equivalent to

//php faces '+' operator, it parses '1/n1' as number
//it stops parsing at '/n' because a number doesn't
//contain this character
echo "1"+1;

      

equivalent to

echo 1+1;

      

+2


source







All Articles