深入理解PHP原理之错误抑制与内嵌HTML
PHP支持内嵌HTML的, 那么对于PHP文件中, PHP标签外的HTML会怎么处理呢? 它和直接输出HTML有什么区别么?
PHP提供了一个错误抑制符’@', 它是通过什么方式来阻止错误输出呢? 我又该在什么时候使用它呢?
这是这俩天一些网友提到的共同问题, 今天就索性整体回答下, 备后来人翻阅.
PHP文件内嵌HTML的处理方式
在PHP中, 所有在标签外的字符, 在词法分析过程中, 都会翻译成T_INLINE_HTML token, 在语法分析的时候, 所有的T_INLIE_HTML都会被分配ZEND_ECHO输出.
也就是说:
- while($con) {
- ?>
- laruence
- }
- ?>
会生成一条OPLINE: T_ECHO, 而操作数是”laruence”;
就结果来说, 上面的代码, 其实和下面的结果一样:
- while($con) {
- echo "laruence";
- }
- ?>
但有一个要注意的地方是, 对于PHP标签外的字符, 在词法分析过程中, 都会以400个字符为单位切分, 比如:
- if(1) {
- ?>
- laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence laruence
- }
- ?>
上面的代码中, 标签外有531个字符(包含空格回车), 会被分成俩条T_INLINE_HTML输出.
错误抑制符
我们知道,在PHP中,可以通过错误抑制符来静默错误提示, 那么它是通过什么方式呢?
在语法分析的过程中, 对于:
- @include('file');
- ?>
会分别在include语句前后插入俩条Opline(操作), 这俩个操作分别做:
1. 保存当前的error_reporting值, 并设置error_reporting(0); //关闭错误输出
2. 恢复之前保存的error_reporting值.
也就是说, 其实上面的代码, 和下面的代码类似:
- $old = error_reporting(0);
- include('file');
- error_reporting($old);
另外, 讲一句题外话:”什么时候才应用错误抑制呢?”, 我个人建议, 就是如果这条语句出错了对你影响不大, 你也不关心这个错误是什么, 你也不会安排额外的逻辑来处理这种错误, 那么你可以使用错误抑制. 否则, 请你使用额外的逻辑来判断错误.

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 is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip
