例如,说:
<?php
// Grab the ID from URL, e.g. example.com/?p=123
$post_id = $_GET['p'];
?>
如何检查变量是否存在$post_id
是一个数字,并且是一个正整数(即 0-9,不是浮点数、分数或负数)?
EDIT:无法使用is_int
'cause $_GET
返回一个字符串。认为我需要使用intval() or ctype_digit(),后者似乎更合适。例如:
if( ctype_digit( $post_id ) ) { ... }
要检查字符串输入是否为正整数,我总是使用该函数ctype_数字。这比正则表达式更容易理解并且更快。
if (isset($_GET['p']) && ctype_digit($_GET['p']))
{
// the get input contains a positive number and is safe
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)