如何优雅的输出PHP调试信息
经常因为出现紧急bug而被老板骂的同事,为了更快的修复而直接利用线上的错误环境现场debug,并直接在页面上echo和dump。结果被老板发现了,又是一通臭骂。那么有没有什么办法更优雅的输出PHP调试信息呢? 这里提供一个方案:利用浏览器调试模式的Console 1,
经常因为出现紧急bug而被老板骂的同事,为了更快的修复而直接利用线上的错误环境现场debug,并直接在页面上echo和dump。结果被老板发现了,又是一通臭骂。那么有没有什么办法更优雅的输出PHP调试信息呢?
这里提供一个方案:利用浏览器调试模式的Console
1,先定义一个php函数:
<span> 1</span> <span>function</span> console(<span>$log</span>=''<span>) </span><span> 2</span> <span>{ </span><span> 3</span> <span>switch</span> (<span>empty</span>(<span>$log</span><span>)) { </span><span> 4</span> <span>case</span> <span>False</span>: <span> 5</span> <span>$out</span> = json_encode(<span>$log</span><span>); </span><span> 6</span> <span>$GLOBALS</span>['console'] .= 'console.log('.<span>$out</span>.');'<span>; </span><span> 7</span> <span>break</span><span>; </span><span> 8</span> <span> 9</span> <span>default</span>: <span>10</span> <span>echo</span> '<script type="text/javascript">'.<span>$GLOBALS['console'].'</script>'<span>; </span><span>11</span> <span> } </span><span>12</span> }
2,在需要调试的地方直接调用它,递交的参数可以是字符串,也可以是数组。
3,在适当的位置调用无参数的console,用来输出调试信息,比如各类php框架入口文件的最底部
4,打开浏览器(比如chrome),载入页面后进入调试模式(比如按F12键),点击Console,你会发现所有数据都在这里等着你查阅。
5,其他用途
优秀的PHP开源框架ThinkPHP提供了输出运行状态的功能,你可以通过运行状态来了解某个页面在后台处理了多长时间。但是,很多人都注意到了,它是不准确的。
下面我们试着用刚刚加进来的自定义函数console在浏览器中输出真正的运行时间。
所有工作都在入口文件中完成:
在所有代码的最开头,定义变量$s=microtime(true);
在console();的上面再增加console('exetime:'.round(microtime(true)-$s,4));
好了,我们来看看真正的运行时间吧:
这个课题就这么简单愉快的解决了。
PS:因为这个函数用到了json来输出数组,所以要求编码为UTF-8,如果使用其它编码可能会出现未知问题,需要留意。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
