1

I'm working on a tree structure in PHP. When looping through the nodes, an exception is sometime thrown because some node that should not be null is null, more precisely it's set to "&NULL":

array(13) {
  // ...
  // some data...
  // ...
  ["segments"]=>
  NULL
  ["leaf"]=>
  bool(false)
  ["children"]=>
  &NULL
}

Since it's not inside quotes, I assume it's some kind of special value, but what does it mean?

laurent
  • 88,262
  • 77
  • 290
  • 428

2 Answers2

4

It just means that it is a reference to a value NULL

$a = array();

$n = null;
$a[1] =& $n;

var_dump($a); // array(1) { [1]=> &NULL }

If you change $n = null; to $n = 1; - then you'll get &int(1)

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • This is `value` vs. `reference`, right? Not missing anything in application? – Jared Farrish May 29 '12 at 02:14
  • @Jared Farrish: I'm not sure I get you :-S – zerkms May 29 '12 at 02:15
  • I just wonder if `null` is referenced or not, considering the approach. It's not about `null`, it's about the approach. What does PHP allow when it comes to by-value or by-reference? `&` has always troubled me. – Jared Farrish May 29 '12 at 02:20
  • @Jared Farrish: php (as many other programming languages) allows to create "aliases" for the same value in memory. They are called references ;-) It allows to have 2 variable names referred to the same value in memory. So you change the one - and the other is "changed" as well. – zerkms May 29 '12 at 02:21
  • I know what a reference is, but what it is in utility is sometimes different, hence the problem of "by value" and "by reference". This is about PHP's behavior and it's "byzantine" syntax. – Jared Farrish May 29 '12 at 02:22
  • I cannot get you :-( But references behaviour in php is pretty similar to references in c++. I don't see anything special in php – zerkms May 29 '12 at 02:23
  • It's alright, I just don't quite understand how PHP handles references with the `&` operator. – Jared Farrish May 29 '12 at 02:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11851/discussion-between-zerkms-and-jared-farrish) – zerkms May 29 '12 at 02:25
0

It's a reference to a value that is null. "&" is the reference symbol.

Steve
  • 6,618
  • 3
  • 44
  • 42