是否可以写入字符串或日志到控制台?

我的意思是

就像在JSP中一样,如果我们打印system.out.println("some")这样的东西,它将出现在控制台,而不是页面上。


当前回答

这里有一个方便的函数。它使用起来超级简单,允许您传递任意类型的任意数量的参数,并将在浏览器控制台窗口中显示对象内容,就像从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请求。

其他回答

这两个都有效:

<?php
    $five = 5;
    $six = 6;
?>
<script>
    console.log(<?php echo $five + $six ?>);
</script>


<?php
    $five = 5;
    $six = 6;
    echo("<script>console.log($five + $six);</script>");
?>

我认为它可以被用来

function jsLogs($data, $isExit) {
    $html = "";
    $coll;

    if (is_array($data) || is_object($data)) {
        $coll = json_encode($data);
    } else {
        $coll = $data;
    }

    $html = "<script id='jsLogs'>console.log('PHP: ${coll}');</script>";

    echo($html);

    if ($isExit) exit();
}

# For String
jsLogs("Testing string"); #PHP: Testing string

# For Array
jsLogs(array("test1", "test2")); # PHP: ["test1","test2"]

# For Object
jsLogs(array("test1"=>array("subtest1", "subtest2"))); #PHP: {"test1":["subtest1","subtest2"]}

干净、快速、简单,没有无用的代码:

function consolelog($data) {
    echo "<script>console.log('".$data."');</script>";
}
$variable = "Variable";
echo "<script>console.log('$variable');</script>";

PHP和JavaScript交互。

我正在寻找一种方法来调试我正在开发的WordPress插件中的代码,然后看到了这篇文章。

我从其他回复中选取了最适合我的代码,并将它们组合成一个函数,我可以用它来调试WordPress。函数为:

function debug_log($object=null, $label=null, $priority=1) {
    $priority = $priority<1? 1: $priority;
    $message = json_encode($object, JSON_PRETTY_PRINT);
    $label = "Debug" . ($label ? " ($label): " : ': ');
    echo "<script>console.log('" . str_repeat("-", $priority-1) . $label . "', " . $message . ");</script>";
}

用法如下:

$txt = 'This is a test string';
$sample_array = array('cat', 'dog', 'pig', 'ant', 'fly');
debug_log($txt, '', 7);
debug_log($sample_array);

如果这个函数用于WordPress开发,那么这个函数应该放在子主题的functions.php文件中,然后可以在代码的任何地方调用。