Use the Javascript console of the browser to debug the PHP program

Source: Internet
Author: User
Nowadays, more and more browsers have developed this tool or Javascript console, we can easily display the variables or array values in PHP code. PHP is a server-side scripting language used to develop dynamic web applications. Compared with JAVA, the absence of a good server-side debugging tool is one of its limitations. Generally, we add echo, var_dump, and other statements in PHP code to display the values of variables and arrays in the browser for debugging.

Nowadays, more and more browsers have developed this tool or Javascript console. with these tools, we can easily display variables or array values in PHP code. Here is an example. The PHP code in this example has four tracking levels: info, warn, log, and error. developers can use the browser console to display error variables and array values.

Copy the following code to the php file and save it as WebConsole. php.
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 function.
The code is as follows:
Require_once ('webconsole. php ');
$ Fruits = array ('apple', 'mange', 'bana ');
WebConsole: log ($ fruits );
WebConsole: info ($ fruits );
WebConsole: warn ($ fruits );
WebConsole: error ($ fruits );
?>

Open your browser console and you will find a screen similar to the following:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.