如何回显$ GLOBAL的内容?

时间:2015-10-17 19:00:10

标签: php

我正在尝试打印$ GLOBAL变量的内容,但我的代码会出错。请指导我。

<?php
    echo gettype($GLOBALS); // confirming the type of $GLOBALS
    foreach ($GLOBALS as $value)
        echo $value."<br>";
?>

输出:

array
Notice: Array to string conversion in C:\xampp\htdocs\first.php on line 4
Array

Notice: Array to string conversion in C:\xampp\htdocs\first.php on line 4
Array

Notice: Array to string conversion in C:\xampp\htdocs\first.php on line 4
Array

Notice: Array to string conversion in C:\xampp\htdocs\first.php on line 4
Array

Notice: Array to string conversion in C:\xampp\htdocs\first.php on line 4
Array

Notice: Array to string conversion in C:\xampp\htdocs\first.php on line 4
Array
Array to string conversion

2 个答案:

答案 0 :(得分:1)

使用此功能,它将立即打印出来。

function printArray($array) {
    if (is_array($array)) {
        foreach ($array as $key => $value) {
            //Check if the Value is not recursive
            if ($key != 'GLOBALS') {
                //If the returning value is Array than call back function
                if (is_array($value)) {
                    // recursive function
                    printArray($value);
                } else {
                    //Once returning value is not Array
                    echo $value."<br>";
                }
            }
        }
    }
}

//Calling the function
printArray($GLOBALS);

答案 1 :(得分:-1)

试试这个,&#34; print_r($ GLOBAL)&#34;因为,我目前专注于jsp,我无法谨慎地说这会起作用,但试试上面的说法,它可能有效,因为我有可能我已经解决了,我猜....我不记得我什么时候工作......