我们需要看看Javascript中对象有什么方法/字段。


当前回答

console.log(OBJECT|ARRAY|STRING|...);
console.info(OBJECT|ARRAY|STRING|...);
console.debug(OBJECT|ARRAY|STRING|...);
console.warn(OBJECT|ARRAY|STRING|...);
console.assert(Condition, 'Message if false');

这些应该在谷歌Chrome和Mozilla Firefox上正常工作(如果你使用的是旧版本的Firefox,那么你必须安装Firebug插件) 在ie8或更高版本上,您必须执行以下操作:

启动“开发人员工具”,点击F12按钮 在标签列表中,点击“脚本”标签 点击右边的“控制台”按钮

欲了解更多信息,请访问此URL: https://developer.chrome.com/devtools/docs/console-api

其他回答

下面是我最喜欢的var_dump/print_r等效Javascript php var_dump。

function dump(arr,level) {
    var dumped_text = "";
    if(!level) level = 0;
    
    //The padding given at the beginning of the line.
    var level_padding = "";
    for(var j=0;j<level+1;j++) level_padding += "    ";
    
    if(typeof(arr) == 'object') { //Array/Hashes/Objects 
        for(var item in arr) {
        var value = arr[item];
            if(typeof(value) == 'object') { //If it is an array,
                dumped_text += level_padding + "'" + item + "' ...\n";
                dumped_text += dump(value,level+1);
            } else {
                dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
            }
        }
    } else { //Stings/Chars/Numbers etc.
        dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
    }
    return dumped_text;
}

这是我的解决方案。它很好地复制了var_dump的行为,并允许嵌套对象/数组。注意,它不支持多个参数。

function var_dump(variable) { let out = ""; let type = typeof variable; if(type == "object") { var realType; var length; if(variable instanceof Array) { realType = "array"; length = variable.length; } else { realType = "object"; length = Object.keys(variable).length; } out = `${realType}(${length}) {`; for (const [key, value] of Object.entries(variable)) { out += `\n [${key}]=>\n ${var_dump(value).replace(/\n/g, "\n ")}\n`; } out += "}"; } else if(type == "string") { out = `${type}(${type.length}) "${variable}"`; } else { out = `${type}(${variable.toString()})`; } return out; } console.log(var_dump(1.5)); console.log(var_dump("Hello!")); console.log(var_dump([])); console.log(var_dump([1,2,3,[1,2]])); console.log(var_dump({"a":"b"}));

虽然有点晚了,但这里有一个非常方便的函数,使用起来超级简单,允许您传递任意类型的参数,并将在浏览器控制台窗口中显示对象内容,就像您从JavaScript调用console.log一样——但是从PHP调用的

注意,你也可以使用标签通过传递" tag - yourtag "它将被应用,直到读取另一个标签,例如" tag - yournexttag "

/*
*   Brief:          Print to console.log() from PHP
*   Description:    Print as many strings,arrays, objects, and other data types to console.log from PHP.
*                   To use, just call consoleLog($data1, $data2, ... $dataN) and each dataI will be sent to console.log - note that
*                   you can pass as many data as you want an this will still work.
*
*                   This is very powerful as it shows the entire contents of objects and arrays that can be read inside of the browser console log.
*                   
*                   A tag can be set by passing a string that has the prefix TAG- as one of the arguments. Everytime a string with the TAG- prefix is
*                   detected, the tag is updated. This allows you to pass a tag that is applied to all data until it reaches another tag, which can then
*                   be applied to all data after it.
*
*                   Example:
*                   consoleLog('TAG-FirstTag',$data,$data2,'TAG-SecTag,$data3); 
*                   Result:
*                       FirstTag '...data...'
*                       FirstTag '...data2...'
*                       SecTag   '...data3...' 
*/
function consoleLog(){
    if(func_num_args() == 0){
        return;
    }

    $tag = '';
    for ($i = 0; $i < func_num_args(); $i++) {
        $arg = func_get_arg($i);
        if(!empty($arg)){       
            if(is_string($arg)&& strtolower(substr($arg,0,4)) === 'tag-'){
                $tag = substr($arg,4);
            }else{      
                $arg = json_encode($arg, JSON_HEX_TAG | JSON_HEX_AMP );
                echo "<script>console.log('".$tag." ".$arg."');</script>";
            }       
        }
    }
}

注意:func_num_args()和func_num_args()是php函数,用于读取动态数量的输入参数,并允许该函数从一个函数调用中有无限多个console.log请求

你可以将此用于字符串和对象/数组

function print_r(obj){
   return JSON.stringify(obj, null, "\t"); 
}
console.log(OBJECT|ARRAY|STRING|...);
console.info(OBJECT|ARRAY|STRING|...);
console.debug(OBJECT|ARRAY|STRING|...);
console.warn(OBJECT|ARRAY|STRING|...);
console.assert(Condition, 'Message if false');

这些应该在谷歌Chrome和Mozilla Firefox上正常工作(如果你使用的是旧版本的Firefox,那么你必须安装Firebug插件) 在ie8或更高版本上,您必须执行以下操作:

启动“开发人员工具”,点击F12按钮 在标签列表中,点击“脚本”标签 点击右边的“控制台”按钮

欲了解更多信息,请访问此URL: https://developer.chrome.com/devtools/docs/console-api