如果你想要一个空字符串默认那么首选方法是这些(根据您的需要)之一:
$str_value = strval($_GET['something']);
$trimmed_value = trim($_GET['something']);
$int_value = intval($_GET['somenumber']);
如果URL参数something
并不在URL中存在然后$_GET['something']
将返回null
strval($_GET['something'])
- >strval(null)
- >""
和你的变量$value
设为一个空的字符串。
trim()
可能超过strval()
根据代码者优先(如名称参数可能希望使用它)
intval()
如果只是数值预计,默认值是零。 intval(null)
- >0
情况来考虑:
...&something=value1&key2=value2
(典型值)
...&key2=value2
(参数从网址$ _GET失踪将返回null吧)
...&something=+++&key2=value
(参数" "
)
为什么这是首选的a pproach:
- 它适合在一条线上,并清楚发生了什么事情。
- 这是可读比复制/粘贴错误的
$value = isset($_GET['something']) ? $_GET['something'] : '';
- 降低风险或一个错字:
$value=isset($_GET['something'])?$_GET['somthing']:'';
- 这是与旧的和新的PHP兼容。
更新 严格模式可能需要这样的事情:
$str_value = strval(@$_GET['something']);
$trimmed_value = trim(@$_GET['something']);
$int_value = intval(@$_GET['somenumber']);
相关http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol- mean-in-php – Musa
可能的重复[什么是PHP? :运算符调用,它是做什么的?](http://stackoverflow.com/questions/1080247/what-is-the-php-operator-called-and-what-does-it-do) – Niko