首页 / PHP / 我应该声明并检查PHP中是否存在变量?
我应该声明并检查PHP中是否存在变量?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了我应该声明并检查PHP中是否存在变量?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3541字,纯文字阅读大概需要6分钟。
内容图文
![我应该声明并检查PHP中是否存在变量?](/upload/InfoBanner/zyjiaocheng/815/854ed75043094747a6a68528d0ce2466.jpg)
我在XAMPP上注意到严格的错误报告已经开启,我现在得到了未定义的索引错误.我只有两个小问题(我还在这里学习):
我知道你不必在PHP中声明变量,但是还是有任何优势来声明它们吗?如果没有,当我没有定义严格错误报告时,为什么会出现错误?
例如,当我使用get变量时,我会在运行类似函数之前检查它们的值
if($_GET['todo'] == 'adduser')
runFunctionAddUser();
这会产生错误,因为我从不检查get变量是否首先存在.我应该这样做
if(isset($_GET['todo']))
if($_GET['todo'] == 'adduser')
runFunctionAddUser();
代替?这会有优势还是不必要而且缓慢?
解决方法:
这个帖子很老了,但我做了一些与问题相关的测试,所以我不妨发布它:
测试代码(PHP 5.3.3 – CentOS 6.5版(最终版)):
class NonExistant
{
protected $associativeArray = array(
'one' => 'one',
'two' => 'two',
'three' => 'three',
'four' => 'four',
'five' => 'five',
'six' => 'six',
);
protected $numIterations = 10000;
public function noCheckingTest()
{
for ($i = 0; $i < $this->numIterations; $i++) {
$this->associativeArray['none'];
}
}
public function emptyTest()
{
for ($i = 0; $i < $this->numIterations; $i++) {
empty($this->associativeArray['none']);
}
}
public function isnullTest()
{
for ($i = 0; $i < $this->numIterations; $i++) {
is_null($this->associativeArray['none']);
}
}
public function issetTest()
{
for ($i = 0; $i < $this->numIterations; $i++) {
isset($this->associativeArray['none']);
}
}
public function arrayKeyExistsTest()
{
for ($i = 0; $i < $this->numIterations; $i++) {
array_key_exists($this->associativeArray['none']);
}
}
}
结果是:
| Method Name | Run time | Difference
=========================================================================================
| NonExistant::noCheckingTest() | 0.86004090309143 | +18491.315775911%
| NonExistant::emptyTest() | 0.0046701431274414 | +0.95346080503016%
| NonExistant::isnullTest() | 0.88424181938171 | +19014.461681183%
| NonExistant::issetTest() | 0.0046260356903076 | Fastest
| NonExistant::arrayKeyExistsTest() | 1.9001779556274 | +209.73055713%
使用via call_user_func()调用所有函数,并使用microtime调整(true)
意见
empty()和isset()是这里其他2种方法的明显赢家,这两种方法几乎与性能挂钩.
is_null()执行错误,因为它需要首先查找值,几乎与访问不存在的值$this-> associativeArray [‘none’]相同,这涉及到数组的完整查找.
但是,我对array_key_exists()的性能感到惊讶.它比empty()和isset()慢2倍.
注意:
我测试过的所有函数都有不同的用途,这个基准测试仅适用于您希望快速检查数组中值的最通用用例.我们可以讨论是否应将null视为“价值”或仅仅是不存在的指标,但这是另一个主题. o.o
更新2017-01-20
使用PHP 7.1
修复了@bstoney提到的bug
$php -v
PHP 7.1.0 (cli) (built: Dec 2 2016 03:30:24) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies
$php -a
php > $a = ['one' => 1, 'two' => 2, 'three' => 3];
php > $numIterations = 1000000;
php > $start = microtime(true); for ($i = 0; $i < $numIterations; $i++) { $a['none']; }; echo microtime(true) - $start;
0.43768811225891
php > $start = microtime(true); for ($i = 0; $i < $numIterations; $i++) { empty($a['none']); }; echo microtime(true) - $start;
0.033049821853638
php > $start = microtime(true); for ($i = 0; $i < $numIterations; $i++) { is_null($a['none']); }; echo microtime(true) - $start;
0.43995404243469
php > $start = microtime(true); for ($i = 0; $i < $numIterations; $i++) { isset($a['none']); }; echo microtime(true) - $start;
0.027907848358154
php > $start = microtime(true); for ($i = 0; $i < $numIterations; $i++) { array_key_exists('none', $a); }; echo microtime(true) - $start;
0.049405097961426
内容总结
以上是互联网集市为您收集整理的我应该声明并检查PHP中是否存在变量?全部内容,希望文章能够帮你解决我应该声明并检查PHP中是否存在变量?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。