PHP数组打印意外的输出格式

I am trying to print an array in reverse order in PHP 7, but it is printed in a strange way.

<?php

$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);
$arr_temp = fgets($handle);
$arr = explode(" ",$arr_temp);
array_walk($arr,'intval');

$output = "";

for($i = $n - 1; $i>=0; $i--){
    $output .= $arr[$i] . " ";
}

print($output);

?>

With the input:

4
1 2 3 4

I get the output:

4
 3 2 1 

Why is that?

As commented by CodeBrauer, the solution is to trim the input string before calling the explode function:

$arr = explode(" ", trim($arr_temp));