dsfg3241 2017-10-15 15:25
浏览 28
已采纳

什么使变量存在PHP?

I've been looking at the documentation for isset() on PHP's website at https://secure.php.net/manual/en/function.isset.php. The return value for this function is apparently as follows:

Returns TRUE if var exists and has value other than NULL. FALSE otherwise.

What is the difference between a variable which is set to NULL and a variable that is not set? Is it true that if $variable = NULL; isset($variable) would return FALSE, meaning the variable is NOT set?

EDIT: How would I then be able to distinguish between a variable whose value is NULL and a variable which does not exist?

  • 写回答

1条回答 默认 最新

  • dth2331 2017-10-15 16:39
    关注

    What is the difference between a variable which is set to NULL and a variable that is not set?

    Take a look at PHP types comparison table #1 (Comparisons of $x with PHP functions). The lines #2-#4 read:

    | Expression      | gettype() | empty() | is_null() | isset() | if($x) |
    |-----------------+-----------+---------+-----------+---------+--------|
    | $x = null;      | NULL      | TRUE    | TRUE      | FALSE   | FALSE  |
    | var $x;         | NULL      | TRUE    | TRUE      | FALSE   | FALSE  |
    | $x is undefined | NULL      | TRUE    | TRUE      | FALSE   | FALSE  |
    

    Can you spot any difference between them? Me neither.

    There is one difference, though. When a variable $x is undefined, gettype($x), is_null(), if($x) and any code that attempts to get its value triggers a notice ("Notice: Undefined variable: x"). empty() and isset() do not trigger any error, warning or notice when their argument does not exist because their purpose is to classify the presence and value of their argument.

    How would I then be able to distinguish between a variable whose value is NULL and a variable which does not exist?

    Apart from the notice triggered as explained above, there is no difference between a variable whose value is NULL and a variable that doesn't exist. Even the notice is just a hint about a possible error in the code or a way to improve its readability but with or without it, the behaviour is the same.

    I cannot figure out any practical usage of telling apart an undefined variable from a variable that stores NULL.

    What makes a variable exist PHP?

    A variable exist in PHP since it was assigned a value for the first time until it is unset() or until the code exits its scope (whichever comes first).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算