如何使用PHP在浏览器控制台上打印

时间:2019-04-20 18:23:52

标签: php jquery json

我正在测试我的php代码,并希望查看从json获取的文件并检查其显示方式,以便知道如何将代码发送到mysql

php文件

<?php

$json = json_decode(file_get_contents("php://input"));

 $obj = json_decode($json,true);
 print_r($json);
 print_r($obj);
 function debug_to_console( $data ) {
    $output = $data;
    if ( is_array( $output ) )
        $output = implode( ',', $output);

    echo "<script>console.log( 'Debug Objects: " . $output . "' );</script>";
}

debug_to_console( "Test" ); 

debug_to_console($obj);
 print_r("hello");
 echo "<script>console.log('" . json_encode($json) . "');</script>";
 echo "<script>console.log('" . $obj . "');</script>";
 echo "<script>console.log('" . '$obj' . "');</script>";
 var_dump('Hello');
 var_dump($obj);
 var_dump($json);
header("Content-Type: application/json; charset=UTF-8")


$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

 ?>

这些都不打印到控制台上

想要打印到控制台的代码

1 个答案:

答案 0 :(得分:0)

您可以尝试以下功能

function consoleLogs($data) {
    $html = "";
    $coll;
    if (is_array($data) || is_object($data)) {
        $coll = json_encode($data);
    } else {
        $coll = $data;
    }
    $html = "<script>console.log('PHP: ".$coll."');</script>";
    echo($html);
}

您可以将其用作:-

consoleLogs(array("test1", "test2"));