[PHP] echo output array report Array to string conversion solution

code:

<?PHP
echo "Hello World!";

$demoName = array("kexuexiong","xiong");

echo "<pre>";

var_dump($demoName);

echo $demoName; 

print_r($demoName);

echo "</pre>";
?>

Output result:
insert image description here

Warning:
Warning: Array to string conversion in F:\tempProject\demo\index.php on line 11, the reason is that echo cannot output array variables. The solution is:
1. Remove the output;
2. Loop out the value of the array for output

<?PHP

echo "Hello World!";

$demoName = array("kexuexiong","xiong");

echo "<pre>";

var_dump($demoName);
echo PHP_EOL;
for ($i=0; $i < count($demoName); $i++) {
    
     
    echo $demoName[$i].PHP_EOL; 
}

echo PHP_EOL;

print_r($demoName);

echo "</pre>";
?>

Output result:
insert image description here

Guess you like

Origin blog.csdn.net/qq_22744093/article/details/132492517