问题描述
我有一个这样的页面:
http://sitename/gallery.php?page=2
底部有分页链接,我们可以通过这些链接进行浏览.每次点击页码时,它都会发送一个带有参数 page=1 或 page=2 等的 GET 请求......
It has pagination links at the bottom by which we can browse. Everytime the page numbers are clicked, it would send a GET request with parameters page=1 or page=2 and so on ...
当我将这些值从 $_GET 变量存储到 $page 时,它是一个字符串值.我可以像这样使用 (int) 将其转换为整数:
When I store these values to $page from teh $_GET variable, it is a string value. I can convert it to an integer using (int) like this:
if(!empty($_GET['page'])){
$page = (int)$_GET['page'];
echo "Page Number: ".$page;
}
但是我如何确保传递的值是整数而不是其他垃圾?
But how can I make sure that the value passed is an integer only and not other crap?
推荐答案
使用过滤器:
if (null !== ($page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE))) {
// $page is now an integer
}
这还会检查变量是否同时出现在查询字符串中.如果你想区分缺失和无效,你必须去掉 filter_input() 的最后一个参数:
This also checks whether the variable appears in the query string at the same time. If you want to differentiate between missing and invalid you have to leave off the last argument to filter_input():
$page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT);
// $page can be null (not present), false (present but not valid) or a valid integer
这篇关于如何检查 $_GET Array 中的变量是否为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)