Home Backend Development PHP Tutorial Debugging PHP programs using the browser's Javascript console_PHP tutorial

Debugging PHP programs using the browser's Javascript console_PHP tutorial

Jul 13, 2016 am 10:41 AM
javascript php use develop console yes server Browser of program end Script language debug

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 :
Debugging PHP programs using the browser's Javascript console_PHP tutorial

www.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...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
Sesame Open Door Official Website Entrance Sesame Open Door Official Latest Entrance 2025 Sesame Open Door Official Website Entrance Sesame Open Door Official Latest Entrance 2025 Apr 28, 2025 pm 07:51 PM

Sesame Open Door is a platform that focuses on cryptocurrency trading. Users can obtain portals through official websites or social media to ensure that the authenticity of SSL certificates and website content is verified during access.

Ouyi official website entrance Ouyi official latest entrance 2025 Ouyi official website entrance Ouyi official latest entrance 2025 Apr 28, 2025 pm 07:48 PM

Choose a reliable trading platform such as OKEx to ensure access to the official entrance.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

Binance official website entrance Binance official latest entrance 2025 Binance official website entrance Binance official latest entrance 2025 Apr 28, 2025 pm 07:54 PM

Visit Binance official website and check HTTPS and green lock logos to avoid phishing websites, and official applications can also be accessed safely.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

How to configure the character set and collation rules of MySQL How to configure the character set and collation rules of MySQL Apr 29, 2025 pm 04:06 PM

Methods for configuring character sets and collations in MySQL include: 1. Setting the character sets and collations at the server level: SETNAMES'utf8'; SETCHARACTERSETutf8; SETCOLLATION_CONNECTION='utf8_general_ci'; 2. Create a database that uses specific character sets and collations: CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci; 3. Specify character sets and collations when creating a table: CREATETABLEexample_table(idINT

Laravel Live Chat Application: WebSocket and Pusher Laravel Live Chat Application: WebSocket and Pusher Apr 30, 2025 pm 02:33 PM

Building a live chat application in Laravel requires using WebSocket and Pusher. The specific steps include: 1) Configure Pusher information in the .env file; 2) Set the broadcasting driver in the broadcasting.php file to Pusher; 3) Subscribe to the Pusher channel and listen to events using LaravelEcho; 4) Send messages through Pusher API; 5) Implement private channel and user authentication; 6) Perform performance optimization and debugging.

See all articles