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 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类