PHP is a server-side scripting language used to develop dynamic web applications. Compared to JAVA, not having a good server-side debugging tool is one of its limitations. Usually we add echo, var_dump and other statements to the PHP code to display the values of variables and arrays in the browser for debugging purposes.
Now, more and more browsers have development tools or Javascript consoles. Through these tools, we can easily display variables or array values in PHP code. Let's do an example below. The PHP code in the example has four tracing levels: info, warn, log, error. Developers can use the browser console to display error variables and array values.
Copy the following code into the PHP file and save it as WebConsole.php
Copy the code The code is as follows:
class WebConsole {
private static function write($data, $type = 'info') {
$method_types = array('error', 'info' , 'log', 'warn');
$msg_type = ''; (PS: T good PHP Q buckle: 304224365, verification: csl)
if(in_array($type, $method_types)) {
$msg_type = sprintf("console.%s", $type);
}else {
$msg_type = sprintf("console.%s", 'info');
}
if(is_array($data)) {
echo("<script>$msg_type('".implode(', ', $data)."');</script> ");
} else {
echo("<script>$msg_type('".$data."');</script>");
}
}
public static function info($data) {
self::write($data);
}
public static function error($data) {
self:: write($data, 'error');
}
public static function log($data) {
self::write($data, 'log');
}
public static function warn($data) {
self::write($data, 'warn');
}
}
?>
Now, import the WebConsole class and use the tracking functionality.
Copy code The code is as follows:
require_once('WebConsole.php');
$fruits = array('apple', 'mange', 'banana');
WebConsole::log($fruits);
WebConsole::info($fruits);
WebConsole::warn ($fruits);
WebConsole::error($fruits);
?>
Now open your browser console and you will find a screenshot similar to the one below :
http://www.bkjia.com/PHPjc/676884.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/676884.htmlTechArticlePHP is a server-side scripting language used to develop dynamic web applications. Compared to JAVA, not having a good server-side debugging tool is one of its limitations. Usually we are in PHP code...