使用PHP 5.3?:运算符 [英] Using PHP 5.3 ?: operator
本文介绍了使用PHP 5.3?:运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用此测试页:
$page = (int) $_GET['page'] ?: '1';
echo $page;
当页面未定义时,我不明白我得到的输出:
I don't understand the output I'm getting when page is undefined:
Request Result
?page=2 2
?page=3 3
?page= 1
? error: Undefined index page
为什么出现错误信息?它是PHP 5.3;为什么不回显"1"?
Why the error message? It's PHP 5.3; why doesn't it echo "1"?
推荐答案
(在我看来)正确的方法是:
The proper way (in my opinion) would be:
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
即使您使用了新样式,也会出现?page=0
的问题(因为0
的值为false). 新" 并不总是更好……您必须知道何时才能使用它.
Even if you used the new style, you would have problems with ?page=0
(as 0
evaluated to false). "New" is not always better... you have to know when to use it.
这篇关于使用PHP 5.3?:运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文