对于php中的整数变量和字符串变量的规则,我有点不清楚。
如果我正在处理计数器(简单的整数从1开始并向上计数),是否需要将变量指定为整数,然后在使用时将它们转换为字符串,或者是否可以将它们保留为字符串,然后在php运行时自动进行转换?
还是有太多的具体案例有不同的规则?有些想法会很感激..。
发布于 2014-09-16 02:02:19
PHP
在需要时自动转换valriables
。你只是需要小心,当你要比较他们。
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.
看看这
发布于 2014-09-16 01:52:54
PHP中没有变量声明,变量将接受您分配给它的数据的类型。
$var = 1; //here $var is an int
$var = '1'; // now is a string
$var = true; //and now a boolean
https://stackoverflow.com/questions/25865635
复制