PHP - 为什么在浏览器中查看时 echo 语句和 print_r() 函数的输出会有所不同?请解释一下


PHP - Why there is a difference in the output of the echo statement and the print_r() function when viewed in a browser? Please explain

<?php
$cookies_data = array(
    'user_1' => array(
        '$user_name' => 'xyz khan', 
        '$user_email' => 'xyz@yahoo.se'
    ),
    'user_2' => array(
        '$user_name' => 'abc khan',
        '$user_email' => 'abc@gmail.com'
    ),
    'user_3' => array(
        '$user_name' => 'rst khan',
        '$user_email' => 'rst@lovemail.com'
    )
);
foreach ($cookies_data as $key1 => $value1) {
    $temp_key1 = $key1;
    foreach ($cookies_data[$temp_key1] as $key2 => $value2) {
        echo "<BR>$key2------------------$value2<BR>";//This line is for debugging 
        setcookie($key2, $value2, (time()+30), "/");//expire cookies after 30 seconds.
    }
}
    //Checking for debugging purpose if the cookies have been set.
    echo '<pre>', print_r($_COOKIE, TRUE), '</pre>';
?>

echo 语句确保 $key 2 和 $value 2 中的值是正确的,并且它们作为参数在 setcookies(( 方法的下一行中提供,但浏览器中的输出不同。在浏览器中,我们只获取最后user_naem和user_email设置,但 print_r(( 方法不显示其他值。

每个user_nameuser_email cookie 都会替换该名称的上一个 cookie,因为它是相同的 cookie 名称。在循环结束时,将仅保留最近设置的值。