Why php does not complain when referencing a non existing variable?

前端 未结 2 340
迷失自我
迷失自我 2021-01-12 08:21

I was wondering why php does not complain when we reference a non existing variable (being it a plain variable or array), is this just the way it is, or there is something e

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-12 09:12

    throws no warning about a non existing variable.

    This is how references work. $a = &$b; creates $b if it does not exist yet, "for future reference", if you will. It's the same as in parameters that are passed by reference. Maybe this looks familiar to you:

    preg_match($pattern, $string, $matches);
    

    Here the third parameter is a reference parameter, thus $matches does not have to exist at time of the method invocation, but it will be created.

    that &NULL is something I didn't really expected, I thought I would get a plain NULL.

    Why didn't you expect it? $r['er'] is a reference "to/from" $t. Note that references are not pointers, $r['er'] and $t are equal references to the same value, there is no direction from one to the other (hence the quotation marks in the last sentence).

提交回复
热议问题