So with this code:
$x = 'hello world';
$y = 'bye world';
$arr = array ($x, $y);
$arr[0] = 'new string';
var_dump($x);
$arr[0] = 'new string'; it's not changing the value of $x. How can I change $x value referencing the array position?
So with this code:
$x = 'hello world';
$y = 'bye world';
$arr = array ($x, $y);
$arr[0] = 'new string';
var_dump($x);
$arr[0] = 'new string'; it's not changing the value of $x. How can I change $x value referencing the array position?
You could create a reference to $x, instead of copying the variable. When passing it into the array as you are, you are merely copying the value of $x, and inserting that copy into the array. If you instead pass $x as a reference, using &$x, any updates made will be made to $x instead. Since the value is a reference, it will be updated "both places".
$x = 'hello world';
$y = 'bye world';
$arr = array (&$x, $y);
$arr[0] = 'new string';
var_dump($x);
Output:
string(10) "new string"