我对php中的整数和字符串变量的规则有点不清楚。
如果我正在处理计数器(从1开始并向上计数的简单整数),我是否需要将变量指定为整数然后在我使用时转换为字符串,或者我可以将它们保留为字符串并且转换将自动发生在php中在运行时间?
或者是否存在太多具有不同规则的特定案例?一些想法将不胜感激......
答案 0 :(得分:0)
PHP中没有变量声明,变量将采用适合您分配给它的数据的类型。
$var = 1; //here $var is an int
$var = '1'; // now is a string
$var = true; //and now a boolean
答案 1 :(得分:0)
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.
看看this