PHP 8.4.24 Released!

Voting

: eight minus two?
(Example: nine)

The Note You're Voting On

info at comfortnet dot de
1 year ago
You should be careful when using pointers. The following example shows how an array is unintentionally distorted if the pointer variable is not treated with unset

<?php

echo '<pre>';

$arr = array ( 'a' => 'a' , 'b' => 'b' ) ;

var_dump ($arr) ;

foreach ( $arr as $index => &$data ) {}

var_dump ($arr) ;

// unset($data);

foreach ( $arr as $index => $data ) {}

var_dump ($arr) ;

?>

Output:

array(2) {
  ["a"]=>
  string(1) "a"
  ["b"]=>
  string(1) "b"
}
array(2) {
  ["a"]=>
  string(1) "a"
  ["b"]=>
  &string(1) "b"
}
array(2) {
  ["a"]=>
  string(1) "a"
  ["b"]=>
  &string(1) "a"
}

<< Back to user notes page

To Top