PHP-the difference between commas and dot numbers

Source: Internet
Author: User
php点号(.)和逗号(,)作用都是链接字符串 echo ‘点‘.‘号‘; //用点号连接字符串 输出 点号echo ‘逗‘,‘号‘; //用逗号连接字符串 输出 逗号效果是一样的,但还是有很大区别echo ‘1+99=‘ . 1+99; //输出100输出的结果是100 而不是1+99=100echo "1+99=" . 99+1; //输出2当把1和99换下位置.结果就变成了2当把点号换成逗号echo ‘1+99=‘ ,99+1; //输出 1+99=100 echo ‘1+99=‘ , 1+99; //输出 1+99=100 只有使用逗号得到结果才是想要的结果那为什么点号就不行呢?逗号为什么就行呢?echo ‘1+99=‘ . 99+1; 输出2可以看出php字符串连接从左到右进行,所以是先连接字符串成"1+99=99"然后再加1 ,即为"1+99=99"+1(字符串于数字相加)那为什么就会输出2呢?这个跟PHP字符串到数字的转换,从左到右截取,知道碰到了不合法的数组,截取出来的部分转成数字,从左到右截取到第一个不是数字为止,如果第一个值不是数字就直接返回0echo (int)‘ccc100‘; //输出0 echo (int)‘1abc‘; //输出1 echo (int)‘10abc‘; //输出10echo (int)‘100abc‘; //输出100 字符串"1+99=99" 强制类型转换后得到的值就应该是1,在此基础上+1 1+1当然是2了echo ‘5+1=‘ . 1+5; //输出10 echo ‘5+1=‘ . 5+1; //输出6 echo ‘1+5=‘ . 1+5;//输出6 echo ‘1+5=‘ . 5+1;//输出2 为什么使用逗号就没有上面的问题了呢?官方手册上解释.逗号是multiple parameters(多参数)逗号分隔开就相当于是N个参数,也就是说把echo当做函数用echo先对每个参数先进行计算,最后再进行连接后输出所以逗号不存在上面的问题

PHP-the difference between commas and dot numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.