Comparing the same function in php and js

I have 2 different but kind of the same function. One is made in php, the other is made in javascript. They both look the same to me. Yet they both do different things. I want to know why. Is it because these functions behave differently in both languages or is it they way they work with variables?

I have one file saved as test.php. It looks like this:

$test= [5,6,7,8];
function testing(){
	$test = [1,2,3,4,5];
	echo 'the function worked <br>';

It gives me back:

the function worked
array(4) { [0]=> int(5) [1]=> int(6) [2]=> int(7) [3]=> int(8) }

Now for about the same function in javascript. This is saved as test.html.

var test= [5,6,7,8];
function testing(){
	test = [1,2,3,4,5];

In my console, I see my array is [1,2,3,4,5]

So my array in php is [5,6,7,8].
My array in javascript is [1,2,3,4,5].
Why does it work different?

I only know the JS part of it. Now at javascript, if you don’t add
"var" to your variable inside your function, Js is starting to look one level down on its scope, until to global scope.

So at your example, it is going one level down and assigning the test at global scope. now you change the state of your test variable at global scope.

I believe PHP has a block scope behaviour so you don’t mutate the outer variable.

The short answer is there is no block scope at ES5. But they add let to ES6 to give this behaviour to variables.