PHP的异常类型

Jun 13, 2016 pm 01:13 PM
boolean error nbsp php quot

PHP的错误类型


一,程序本身的错误

 这种错误是比较容易就可以处理的错误,比如说语法错误或者是编译器无法解析,这些错误在编译的时候,就会不通过编译,然后可以根据错误的提示,找到具体的错误代码。但当使用eval()执行的代码发生错误时,在编译期间是发现不了,它只能等到代码在执行的过程中,对其编译产生错误,如我们执行下列代码,就会在执行过程中出现错误:

<span style="font-size:18px;"><?php echo "this will be echo to putty";
      eval("php error when parser");

?></span>
Copy after login
这种错误和其它的代码错误不同的是,在位于它之前的echo是可以输出的。而其它的代码错误是不会有任何输出的,因为编译就通不过,不会运行。


二,未定义符号

当PHP执行的时候,它可能遇到许多变量、函数等它不知道的名字,因为PHP在编译的时候,并没有完整的了解所有的符号名称、函数名等。

如果只是未定义的常量或者变量,这是只是一个通知的形式告知。但如果是遇到未定义的函数或者类,程序将会终止运行。在关于找不到类的时候,如果用户定义了叫做__autoload的函数,它将在PHP遇到一个未定义的类时调用,如果通过这个函数可以返回类,新加载的类将会被使用,不产生任何错误。


三,通用性错误

3.1 操作系统所带来的差异

一些只在某特殊的平台可用的PHP函数。

一些在某特殊的平台不可用的PHP函数。

一些在不同的平台下有着细微差别的PHP函数。

区别文件名中的路径成份的字符。

外部程序或者服务并不是在所有平台中都可用。

3.2 PHP配置差异

比如配置选项magic_quotes_gpc,如果这个选项是开启的,PHP将增加斜线到所有的外部数据中。这时,如果将程序移植至另一台没有开启这个选项的服务器上上时,用户的输入就会有问题了。处理类似差异的办法是检查PHP代码并通过ini_get()函数查看选项是否启用,然后作一个统一的调整。

register_globals : 该设置决定PHP是否引入GET,POST,cookie,环境变量或者服务器变量为全局变量。一般避免使用。

allow_url_fopen: 如果这个选项设置为false,对URL文件操作的功能是关闭的。

3.3 SAPI差异


四,运行错误 

如对硬盘数据或者网络操作以及数据库调用时,由于PHP本身以外的因素所造成的错误。

五,PHP错误 

PHP中的错误机制是被所有的PHP内置函数使用的,通常这个简单的机制打印出一个错误信息,包括一些错误相关的基本信息。

错误级别分为:

E_ERROR:严重错误。

E_WARNING:最普通的错误类型。

E_PARSE:解析错误在编译的时候发生。

E_STRICT:这个错误级别是唯一不包含在E_ALL常量中的,为了让PHP4到PHP5的迁移更加容易。

E_NOTICE:表示运行的代码可能在操作一些未知的事情。

E_CORE_ERROR:由于扩展启动失败等导致的。

E_COMPILE_WARNING:编译的时候出现的警告,告诉用户一些不推荐使用的语法信息。

E_COMPILE_ERROR:编译错误。

E_USER_ERROR

E_USER_WARNING

可以用error_reporting(Integer)函数来设置哪些错误被报告。如所有的错误,表示为E_ALL ,除了通告以外的所有错误,可以表示为E_ALL & ~E_NOTICE.

display_errors(boolean):这个设置控制错误是否作为PHP输出的一部分显示出来。

log_errors(boolean),这个设置控制错误是否记录。日志的地址是通过error_log(String)设置决定的。默认情况下,错误被记录到WEB服务器的错误日志中。

html_errors(boolean):设置控制是否在错误信息中采用HTML格式 。

六,定义错误处理器

可以调用 set_error_handler(error_handle_function,error_types)指定错误处理器。如

function customError($errno, $errstr, $errfile, $errline)
 { 
 echo "Custom error: [$errno] $errstr
";
 }

set_error_handler("customError"); // 默认为所有的错误,就是E_ALL.

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
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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 in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

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.

See all articles