PHP Conference Kansai 2025

Voting

: seven plus zero?
(Example: nine)

The Note You're Voting On

mip at ycn dot com
18 years ago
Ever wounder what array_splice is doing to your references, then try this little script and see the output.

<?php

$a
= "a";
$b = "b";
$c = "c";
$d = "d";
$arr = array();
$arr[] =& $a;
$arr[] =& $b;
$arr[] =& $c;
array_splice($arr,1,0,array($d));
$sec_arr = array();
$sec_arr[] =& $d;
array_splice($arr,1,0,$sec_arr);

$arr[0] = "test"; // should be $a
$arr[3] = "test2"; // should be $b
$arr[1] = "this be d?"; // should be $d
$arr[2] = "or this be d?"; // should be $d
var_dump($arr);
var_dump($a);
var_dump($b);
var_dump($d);
?>

The output will be (PHP 4.3.3):

array(5) {
[0]=>
&string(4) "test"
[1]=>
&string(10) "this be d?"
[2]=>
string(13) "or this be d?"
[3]=>
&string(5) "test2"
[4]=>
&string(1) "c"
}
string(4) "test"
string(5) "test2"
string(10) "this be d?"

So array_splice is reference safe, but you have to be careful about the generation of the replacement array.

have fun, cheers!

<< Back to user notes page

To Top