Common PHP Errors
常见PHP错误
December 7, 2007 - 10:44 pm | Nessa (来源,忘了从哪个网站COPY的了)
I’m going back to the basics here, you know, when you wrote your first PHP script and saw an ugly-ass error message pop up on your screen? Error messages are the best tool a programmer has.
我又回到最基础的上面来了,你知道,当你在最初写PHP代码时,你是否经常看到那些可恨(我翻译成可恨了)的出错信息?其实,出错信息对一个程序员来说是一个最好的工具。
Set up Error Reporting
设置出错报告
Most PHP errors are straight forward, but there are times where you don’t see any which makes it very difficult to tell what the problem is.
大多数PHP的错误信息都比较直观,但有时你看到的错误信息却很难明确的告诉你问题在哪里。
The first step of PHP troubleshooting is to turn error reporting on. For security reasons you’ll want error reporting off by default, but if something goes wrong you’ll need the information for debugging. You can usually enable error reporting by adding this line to the problem script:
首先你得打开你的 PHP出错报告。从安全角度来说,你可能会想到关闭它。但是如果程序出了问题,而你又需要debug的时候,你可以通过以下的PHP代码临时来打开出错信息。
<?php error_reporting(E_ALL) ?>
Or you can add these lines to the root .htaccess:
或者,你可以将以下几行加入到.htaccess文件里
php_flag display_errors on
php_value error_reporting 6143
This will usually display an error useful for troubleshooting, that is, if the software and your server configuration allows it.
如果软件或者服务器支持,这可以显示一些有用的错误信息
例如:
Parse Errors
解析错误
Parse error: parse error, unexpected T_STRING in……
This is a syntax error. Perhaps you forgot a semi-colon at the end of a line, or you forgot a double quote (”) or an end bracket (}) after you started one. For quote and semicolon issues, the problem is usually the line above the one reported in the error. For brackets, it may be at the end of the script.
这是一个语法构造错误。(这段翻译我想不出怎么好,自己根据理解翻译)出现这段信息的时候,往往都在出错代码的上一行,请检查一下代码的结束,是否双引号或者大括号不匹配,有时候也会有一些字符串的问题在里面。
Parse error: syntax error, unexpected $end in
You’re most likely missing a } somewhere. Make sure that each { you have is also closed with a }.
这东西是忘记了“}”,请检查“{}”是否一一对应了。
Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’ in..
There may be double quotes within double quotes. They either need to be escaped or brought to single quotes. It’s also possible that a new PHP statement was started before the previous was finished.
出现这种问题往往是在双引号中又有了双引号,可能需要在双引号前加上转义符或者使用单引号。通常问题在显示的行数之上一行。
Header Errors
头出输出错误
Warning: Cannot add header information - headers already sent by (output started at /home/vnessa5/www/errors.php:9) in….
Warning: Cannot send session cache limiter - headers already sent in somefile.php on line 222
Naturally, HTML will parse before PHP. The script is trying to send header information after you’ve already sent output to the browser. HTTP headers are required to be sent before any output from your script, which means that a header function must be placed before any html or even a white space. There are two solutions for this. Either (1) Set the header tags the top of the document, or (2) insert a header redirect by adding this to the very top of the page to force the output buffer:
一般情况下,HTML的解析都会在PHP前面,而代码尝试在发送header相关信息时,你已经有了HTML输出。所以你在发现有这种情况出现的时候,请检查你的代码以保证没有HTML代码或者甚至是一个空格。一般有两个解决方案。1)在文件头部设定HEADER标签。2)在你要输出的页面前插入HEADER标签的内容。
<?php ob_start();
Then this at the very end of the page (not usually required)
ob_end_flush(); ?>