It was very difficult to come up with an informed title for this post.
Though PHP and JS are totally different languages, I am very surprised to find that altering an array passed into a function as an argument gives different results.
PHP
<?php
function thing($arr) {
$arr[2] = "WOOF";
}
$hello = array(1,2,3,4);
thing($hello);
print_r($hello);
// Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
?>
Javascript
function thing($arr) {
$arr[2] = "WOOF";
}
$hello = [1,2,3,4];
thing($hello);
console.log($hello);
// [1, 2, "WOOF", 4]
Which is "correct"?
Why is there a difference in results here? Why does JS accept that the argument is simply an alias to the original array, but PHP doesn't?
Which way is the most 'correct' -- and why?