debugging PHP programs using the browser's JavaScript console _php tutorial

Source: Internet
Author: User
PHP is a server-side scripting language used to develop dynamic Web applications. There is no good server-side debugging tool that is one of its limitations compared to Java. Usually we are in the PHP code to add Echo, var_dump and other statements, the variable, the value of the array is displayed in the browser to achieve the purpose of debugging.

Now, more and more browsers have developed the tool or JavaScript console, through which we can easily display the PHP code in the variable or array values. Let's do an example here. The PHP code in the example has four tracking levels: info, warn, log, error, developers can use the browser console to display error variables, array values.

Copy the code below into the PHP file and save it as webconsole.php
Copy CodeThe 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-button 峮: 304224365, verify: 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 ("");
} else {
Echo ("");
}
}

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 trace feature.
Copy CodeThe 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 notice that a screen similar to the following appears:

http://www.bkjia.com/PHPjc/676884.html www.bkjia.com true http://www.bkjia.com/PHPjc/676884.html techarticle PHP is a server-side scripting language used to develop dynamic Web applications. There is no good server-side debugging tool that is one of its limitations compared to Java. Usually we are in PHP code ...

  • 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.