dtpyvb1873 2011-12-03 05:57
浏览 101
已采纳

为什么解释器显示未定义为NULL?

Last time I'm exploring PHP pretty much and I was curious if it's possible to define variable without initializing it like in C++.

Well interpreter doesn't output an fatal eror (only a notice that variable test is undefined) if I'll run this code:

<?php 

$test = (int) $test;

?>

And if I try to check it with var_dump() function i get:

int(0)

I assumed interpreter automatically cast undefined to integer. Well, ok it's pretty clever. But when I removed code repsonsible for type casting and checked it with var_dump() function I get:

NULL

Well, ok. So when I assign undefined variable as undefined variable I get variable with NULL. I can understand interpreter do it for me on the run. But when I try something like this:

<?php

var_dump($test);
var_dump($test);

?>

I get two notices that test is not defined, but var_dump() returns NULL, not undefined. And now I don't get it. If I'll turn off notices var_dump() function will have same result with undefined variables and variables assigned to NULL. And here comes a question from topic. Why interpreter (or rather a var_dump() function) treats undefined and NULL as same ?

  • 写回答

1条回答 默认 最新

  • dpeqsfx5186 2011-12-03 06:04
    关注

    The special NULL value represents a variable with no value. NULL is the only possible value of type NULL.

    A variable is considered to be null if:
    it has been assigned the constant NULL.
    it has not been set to any value yet.
    it has been unset().

    (int)$test = casting, force a value to data type (integer)

    warning notices = cause by $test is never defined, and you trying to use it

    var_dump($test) = I dun have a value for $test, so, I return you a null (by PHP)

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?