PHP “Assign by reference” oddity

后端 未结 2 527
借酒劲吻你
借酒劲吻你 2021-01-17 07:14

I came across a code snippet which included $a = & $b; but hadn\'t tested whether $b actually existed (if (isset($b))). I wasn\'t sure how PHP

2条回答
  •  自闭症患者
    2021-01-17 07:41

    Explanation is as simple as this

    If you assign, pass, or return an undefined variable by reference, it will get created.

    (emphasis mine)

    That's what you're doing; Assigning an undefined index by reference so it gets created.

    Example #1 Using references with undefined variables

    d);
    var_dump(property_exists($c, 'd')); // bool(true)
    ?> 
    

    Example from PHP Manual

    Then you have another question:

    Meanwhile at the same time, $b[11] shows a value NULL and isset()=FALSE even though its referent (apparently) does exist (!)

    That is also explained clearly on the manual

    isset — Determine if a variable is set and is not NULL

    isset() will return FALSE if testing a variable that has been set to NULL

    Since it is NULL, isset() returns FALSE.

提交回复
热议问题