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

标签 php structure undefined isset

我注意到 XAMPP 上启用了严格的错误报告,我现在收到 undefined index 错误。我只是有两个小问题(我还在学习):

我知道您必须在 PHP 中声明变量,但是无论如何声明它们有什么好处吗?如果不是,当我没有定义它们时,为什么在启用严格错误报告时会出现错误?

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

if($_GET['todo'] == 'adduser')
    runFunctionAddUser();

这会报错,因为我从不先检查 get 变量是否存在。我应该怎么做

if(isset($_GET['todo']))
    if($_GET['todo'] == 'adduser')
        runFunctionAddUser();

相反?这样做有什么好处还是没有必要而且速度很慢?

最佳答案

这个帖子有点老了,但我做了一些与这个问题相关的测试,所以我不妨把它贴出来:

测试代码(PHP 5.3.3 - CentOS release 6.5 (Final)):

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('none', $this->associativeArray);
        }
    }
}

结果是:

| 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() 明显优于其他两种方法,这两种方法在性能上非常相关。

is_null() 性能不好,因为它需要先查找值,这与访问不存在的值非常相似 $this->associativeArray['none'],其中涉及对数组的完整查找。

但是,array_key_exists() 的性能让我感到惊讶。它比 empty()isset() 慢 2 倍。

注意:

我测试过的所有函数都有不同的用途,此基准仅适用于您想要快速检查数组中的值的最通用用例。我们可以讨论 null 是否应该被视为“值”或仅仅是不存在的指示符,但这是另一个话题。 o.o

2017-01-20 更新

使用 PHP 7.1

修复了@bstoney 提到的错误

$ 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 中是否存在变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8409249/

相关文章:

javascript - 获取 API 请求的输出(客户端)

php - 图片上传在 byethost 服务器中不起作用

c++ - 将指向结构成员的指针传递给函数

c - 在 C 中按值将结构数组传递给函数

javascript - 从 .each 循环中的 javascript 数组获取 "undefined"

image - 使用 image.Image 或 *image.RGBA 的 Set() 方法

javascript - Ajax-在 PHP 中使用 Json 时出错

php - 为什么查询不能在请求内使用 amp (&)/%26?

mysql - 用于数据库设计的抽象/超/子类结构

javascript - RapidAPI 响应体显示为 "undefined"