简体   繁体   中英

A string in PHP that doesn't make sense

I was experimenting with weak/dynamic typing properties of PHP in preparation for a test and was completely baffled by the output of this string concatenation. Can someone explain how this is even possible?

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

output:

2

Analysis:

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

is equivalent to

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

is 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;

is equivalent to

echo 1+1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM