PHP 8.5.0 Alpha 4 available for testing

Voting

: max(four, nine)?
(Example: nine)

The Note You're Voting On

bravo1romeo
7 years ago
You can reference values of an array to the values of another array,
however if you change the array by reassigning it, the reference will no longer apply, for example:

<?php
$ref
= [1,2,3];
$c = count($ref);
$foo = ['A'];

for(
$i=0;$i<$c;$i++)
$foo[] =& $ref[$i];

print_r($foo);
print_r($ref);

$ref = [4,5,6];

print_r($foo);
print_r($ref);
?>

Will output:
Array
(
[0] => A
[1] => 1
[2] => 2
[3] => 3
)
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Array
(
[0] => A
[1] => 1
[2] => 2
[3] => 3
)
Array
(
[0] => 4
[1] => 5
[2] => 6
)

Therefore if you want the values to still reference you must set the array values individually to not reassign the array:
<?php
$ref
= [1,2,3];
$c = count($ref);
$foo = ['A'];

for(
$i=0;$i<$c;$i++)
$foo[] =& $ref[$i];

print_r($foo);
print_r($ref);

$bar = [4,5,6];
foreach(
$bar as $i => $value)
$ref[$i] = $value;

print_r($foo);
print_r($ref);
?>

Results:
Array
(
[0] => A
[1] => 1
[2] => 2
[3] => 3
)
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Array
(
[0] => A
[1] => 4
[2] => 5
[3] => 6
)
Array
(
[0] => 4
[1] => 5
[2] => 6
)

<< Back to user notes page

To Top