我应该声明并检查PHP中是否存在变量?

Dav*_*hta 4 php structure undefined isset

我在XAMPP上注意到严格的错误报告已经开启,我现在得到了未定义的索引错误.我只有两个小问题(我还在这里学习):

我知道你不具备在PHP声明变量,但它有什么优势无论如何声明呢?如果没有,当我没有定义严格错误报告时,为什么会出现错误?

例如,当我使用get变量时,我会在运行类似函数之前检查它们的值

if($_GET['todo'] == 'adduser')
    runFunctionAddUser();
Run Code Online (Sandbox Code Playgroud)

这会产生错误,因为我从不检查get变量是否首先存在.我应该这样做

if(isset($_GET['todo']))
    if($_GET['todo'] == 'adduser')
        runFunctionAddUser();
Run Code Online (Sandbox Code Playgroud)

代替?这会有优势还是不必要而且缓慢?

Pop*_*lus 7

这个帖子很老了,但我做了一些与问题相关的测试,所以我不妨发布它:

测试代码(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']);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果是:

| 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%
Run Code Online (Sandbox Code Playgroud)

所有函数都以相同的方式调用via call_user_func()和timedmicrotime(true)

意见

empty()并且isset()在这里明显优于其他2种方法,这两种方法几乎与性能挂钩.

is_null()执行不好,因为它需要首先查找值,几乎与访问不存在的值相同$this->associativeArray['none'],这涉及数组的完整查找.

但是,我对它的表现感到惊讶array_key_exists().它是慢2倍empty()isset().

注意:

我测试的所有函数都有不同的用途,这个基准测试仅适用于您希望快速检查数组中值的最通用用例.我们可以讨论是否null应该被视为"价值"或仅仅是不存在的指标,但那是另一个话题.OO

更新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
Run Code Online (Sandbox Code Playgroud)

  • 您应该执行“===null”,而不是 is_null。可能会更快。 (2认同)