PHP中error
今天学习CI框架过程中遇到个问题:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: user
一般在默认的普通PHP文件中输出一个未定义声明的变量是不会报错误的,但在codeigniter框架下却要报错误,这对于想集成 添加 和 修改 页面于一体的”懒人”很不方便,由于是初学者开始还想怎么在代码中屏蔽这一错误提示呢.甚至用到了@,但听很多人都说@会大大降低性能….
最后突然想到,是不是codeigniter有意让这错误信息提示出来了呢,我们该如何去屏蔽掉这一类错误呢无意中搜索到了”如何让codeigniter不显示Notice信息?”,茅塞顿开.原来是入口index.php中的error_reporting(E_ALL);在作怪.只需要把它改成
error_reporting(E_ALL ^ E_NOTICE);
就可以屏蔽掉这个错误,而不影响其他的报错.
下边是搜索到的一些资料:
error_reporting() 设置 PHP 的报错级别并返回当前级别。
语法
error_reporting(report_level)
如果参数 level 未指定,当前报错级别将被返回。下面几项是 level 可能的值:
1 E_ERROR
2 E_WARNING
4 E_PARSE
8 E_NOTICE
16 E_CORE_ERROR
32 E_CORE_WARNING
64 E_COMPILE_ERROR
128 E_COMPILE_WARNING
256 E_USER_ERROR
512 E_USER_WARNING
1024 E_USER_NOTICE
2047 E_ALL
2048 E_STRICT
E_NOTICE 表示一般情形不记录,只有程式有错误情形时才用到,例如企图存取一个不存在的变数,或是呼叫 stat() 函式检视不存在的档案。
E_WARNING 通常都会显示出来,但不会中断程式的执行。这对除错很有效。例如:用有问题的常规表示法呼叫 ereg()。
E_ERROR 通常会显示出来,亦会中断程式执行。意即用这个遮罩无法追查到记忆体配置或其它的错误。
E_PARSE 从语法中剖析错误。
E_CORE_ERROR 类似 E_ERROR,但不包括 PHP 核心造成的错误。
E_CORE_WARNING 类似 E_WARNING,但不包括 PHP 核心错误警告。
PHP 的错误报告
php.ini 文件中有许多配置设置。您应当已经设置好自己的 php.ini 文件并把它放在合适的目录中,就像在 Linux 上安装 PHP 和 Apache 2 的文档说明中所示的那样。在调试 PHP 应用程序时,应当知道两个配置变量。下面是这两个变量及其默认值:
display_errors = Off
error_reporting = E_ALL
通过在 php.ini 文件中搜索它们,可以发现这两个变量当前的默认值。display_errors 变量的目的很明显 —— 它告诉 PHP 是否显示错误。默认值是 Off。但是,要让开发过程更加轻松,请把这个值设为 On:
display_errors = On
error_reporting 变量的默认值是 E_ALL。这个设置会显示从不良编码实践到无害提示到出错的所有信息。E_ALL 对于开发过程来说有点太细,因为它在屏幕上为一些小事(例如变量未初始化)也显示提示,会搞糟浏览器的输出。我只想看到错误和不良编码实践,但是不想看到无害的提示。所以,请用以下值代替 error_reporting 的默认值:
error_reporting = E_ALL & ~E_NOTICE
重新启动 Apache,就全部设置好了。接下来,将学习如何在 Apache 上做同样的事。
服务器上的错误报告
依赖于 Apache 正在做的工作,在 PHP 中打开错误报告可能没法工作,因为在计算机上可能有多个 PHP 版本。有时很难区分 Apache 正在使用哪个 PHP 版本,因为 Apache 只能查看一个 php.ini 文件。不知道 Apache 正在使用哪个 php.ini 文件配置自己是一个安全问题。但是,有一种方法可以在 Apache 中配置 PHP 变量,从而保证设置了正确的出错级别。
而且,最好知道如何在服务器端设置这些配置变量,以否决或抢占 php.ini 文件,从而提供更高级别的安全性。
在配置 Apache 时,应该已经接触过 /conf/httpd.conf 中 http.conf 文件中的基本配置。
要做在php.ini文件中已经做过的事,请把下列各行添加到 httpd.conf,覆盖任何 php.ini 文件:
php_flag display_errors on
php_value error_reporting 2039
这会覆盖在 php.ini 文件中为 display_errors 已经设置的标志,以及 error_reporting 的值。值 2039 代表 E_ALL & ~E_NOTICE。如果愿意采用 E_ALL,请把值设为 2047。同样,还是要重启 Apache。
接下来,要在服务器上测试错误报告。
关于error_reporting()这个函数,它是可以屏蔽到一些错误信息,但是PHP 核心造成的错误,是无法屏蔽的,因为PHP 核心造成的错误会直接导致PHP文件编译失败,因为书写格式没有按照PHP的编码规则写而造成的错误,是无法屏蔽的
复制代码 代码如下:
* For now, avoid warnings of E_STRICT mode
* (this must be done before function definitions)
*/
if (defined('E_STRICT')) {
$old_error_reporting = error_reporting(0);
if ($old_error_reporting & E_STRICT) {
error_reporting($old_error_reporting ^ E_STRICT);
} else {
error_reporting($old_error_reporting);
}
unset($old_error_reporting);
常见的如下:
复制代码 代码如下:
// Turn off all error reporting;关闭所有的错误
error_reporting(0);
// Report simple running errors;报告一个简单的运行错误
error_reporting(E_ERROR E_WARNING E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings …);包括报告一些未初始化的变量或捕捉变量名的拼写错误
error_reporting(E_ERROR E_WARNING E_PARSE E_NOTICE);
// Report all errors except E_NOTICE
// This is the default value set in php.ini;报告所有的错误但不包括E_NOTICE 这也是php.ini的缺省设置
error_reporting(E_ALL ^ E_NOTICE);
// Report all PHP errors (bitwise 63 may be used in PHP 3);报告所有的错误
error_reporting(E_ALL);
// Same as error_reporting(E_ALL);同上
ini_set('error_reporting', E_ALL);

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

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

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.

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.
