Home > Backend Development > PHP Tutorial > 字符串加数字为何结果是数字(面试题)

字符串加数字为何结果是数字(面试题)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 11:47:24
Original
1485 people have browsed it

字符串加数字为什么结果是数字(面试题)
前几天参加一个面试,出了一个题,写出运行结果,题目大概是一个字符串加一个数字被赋值给一个变量,输出变量:
$test = 'linux' + 6;
echo $test;
后来运行了下得知结果其实就是后面的数字,但是还不理解为什么是这样,求指导,谢谢。

------解决方案--------------------
$test = 'linux' + 6;
你是在做加法!
既然是加法,那么字符串也就要转换成数字才能参与运算
而非数字开头的字符串只能被转换为 0
即:
$test = 'linux' + 6;
等价于
$test = 0 + 6;
所以 echo $test; 就输出了 6

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template