PHP intval() weird results
我遇到了一些奇怪的事情,我不知道为什么会这样!
我有一个类似的网址:
http://mysite.com/users/
此用户ID可以为
因此,假设一个用户的ID为
在我的php页面上,我需要查询db,但是在此之前,我需要知道要看哪一列,
所以我想到了这个解决方案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php $id = $_GET['id']; $id_inted = intval($_GET['id']); if($id == $id_inted){ // The column which I should look into is id $column = 'id'; }else{ // The column which I should look into is page_name $column = 'page_name'; } $query = mysql_qyery(SELECT * FROM members WHERE $column = '$id'); ?> |
因此,我对其进行了测试,但是即使调用此URL,结果也很奇怪:
http://mysite.com/users/
发生这种情况:
我打开了一个新的测试页面:
1 2 3 4 5 6 7 | <?php $real_string = 'abcdefg'; $string_inted = intval($real_string); echo"Real String is:" . $real_string ."<br />"; echo"The same string as int is:" . $string_inted ."<br />"; if($real_string == $string_inted) echo"Weird!"; else echo"Fine..."; ?> |
结果:
1 2 3 | Real String is: abcdefg The same string as int is: 0 Weird! |
为什么会这样呢?
提前致谢。
PHP确实与所谓的"类型变戏法""连接"在一起。它是大多数PHP脚本中最容易出错的部分。因此,您应始终保持安全,并使用最可靠的检查方法。例如,
在这种情况下,应结合使用
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php $id = intval($_GET['id']); if($id > 0){ // The column which I should look into is id $column = 'id'; }else{ // The column which I should look into is page_name $column = 'page_name'; } $query = mysql_qyery(SELECT * FROM members WHERE $column = '$id'); ?> |
或者,更短:
1 2 3 4 | $id = intval($_GET['id']); $column = ($id > 0) ?"id" :"page_name"; $query = mysql_qyery(SELECT * FROM members WHERE $column = '$id'); |
Aso注意可能没有设置$ _GET [" id"],这会在您的代码中引发一个通知。
最后但同样重要的是:SQL注入:
编辑正如评论员所指出的那样,我的代码存在逻辑缺陷,这是由于我仅在phpsh中对其进行了测试。我将其从
要检查是否要求数值,请使用
在类型方面,PHP有点奇怪。
基本上,您正在做的是将字符串解析为数字(因此
现在,我明白了为什么您会认为它应该为假,但PHP会尝试变得更聪明。基本上,
最好使用
来自http://php.net/manual/en/language.operators.comparison.php
1 2 3 4 5 6 | Example Name Result $a == $b Equal TRUE if $a is equal to $b after type juggling. $a === $b Identical TRUE if $a is equal to $b, and they are of the same type. |
由于
这解释了为什么代码中的
如果使用
成功时为var的整数值,失败时为0的整数值。空数组返回0,非空数组返回1。(来自php.net intval()。)
intval('abcdefg')将触发错误,并且函数返回0。