Common php errors and how to debug them

A lot of questions asked by beginners are about errors errors that occur when running a PHP script. In the following article, I will try to summarize these errors, hoping to be useful to those who, like me when I’ve started coding in PHP,met with such problems.

PHP Parse error: syntax error, unexpected T_[[word]]

(word might be VARIABLE, STRING, NUM_STRING, ENCAPSED_WHITE_SPACE, IF, LNUMBER, STATIC, PRINT etc )
You should look for syntax error on the line mentioned in the error, and also in the line before that. You should look for unclosed quotes, incomplete assingments, missing semicolon, missing curly braces…

echo "$arr['index'], do something";

will produce T_ENCAPSED_AND_WHITESPACE error. You should replace with something like:

echo $arr['index'].", do something";

Parse error: syntax error, unexpected $end

Here, the line is of no use (it’s just the last line from the script), so this is one of the hardest errors to debug. Usually this means that is a bracket not closed somewhere inside your script, and php expect to find that closing bracket when suddenly it reaches for the end tag (?>)

function foo(){
$sum=0;
for ($i=0;$i<=10$i++){
$sum+=$i;
}
return $sum
//Note the missing } here
?>

Invalid argument supplied for foreach

This usually means that foreach takes an input that is not an array

Solution is to check first to see if the variable supllied to foreach is a non-empty array:

if (is_array($input) && !empty($input)){
foreach ($input as $key=>$value){
....
}
}

Using $this when not in object context

This means either you are using $this outside of class:

class Foo{
var $bar;
function myMethod(){
//Correct way
$this->bar = 1;
}
}
//Fatal error using $this when not in object context
$this->bar=1;

or you are calling it staticly:

Foo::myMtethod();

Failed to open stream no such file or directory

Usually it means that the file does not exists. Check for misspelling, correct letter case (most servers are case sensitive) or permissions. If you use relative path link like ‘includes/libary’ (which i don’t recommend to do) check to see how php translate this path using realpath: realpath(‘includes/library’);

Call to undefined function …

Check the spelling of the function call. If the spelling is correct, and you are using internal php functions, check to see if that function is available for your PHP version (you can check your php version with phpversion() )

Call to a member function … on a non-object

It means you either did not intialize the object, or you misspelled the name of the object.

class Foo{
	var $bar;
	function myMethod(){
		$this->bar = 1;
	}
}
 
//$foo is not initalized, you should call $foo=new Foo(); to work
$foo->mYMethod();

Cannot modify header information – headers already sent output started in ….

This means that you are trying to set some headers, or starting a session, after you ouptut something from your php script. PHP tells you where the output started, so you should look there for any output produced.  You should also keep in mind, that some editors, add output at the begining of the file, even if it’s not visible to you. If this is your case, you should stop using that editor :)

SAFE MODE Restriction in effect. The script whose uid is … is not
allowed to access … owned by uid … in …

Open_basedir restriction in effect. File is in wrong directory in …

This is one of the common errors that you will see if you have safe_mode is activated

The first one means that the owner of the script (apache) does not have the rights to access a directory created by other user (when you create a folder using ftp).

The second one means that only some folders (configured in open_basedir) can be accessed by your php script.

The solution is to try to create all folders for upload, using php script, moving all your php script to a folder that is accepted in the open_basedir settings, or ask your webhost to change the open_basedir setting on your server.

Allowed memory size of … bytes exhausted (tried to allocate … bytes) in …

Maximum execution time of … seconds exceeded in …

Again, here the line doesn’t say to much about the error. It means that in that line, the script run out of resources allocated, but it could be different on other runs.

What you should do is look into your site for variables that uses a lot of memory.

Ex:

$query = "SELECT * FROM articles";
$result = mysql_query($query);
while ($row=mysql_fetch_assoc($result)){
$data[] = $row;
}

on a table with 100.000 articles will be a memory killer, since the $data variable will keep all those.

Try to limit your query and unset all your varibales after using them.

If nothing else works, you can try cahnging the memory limit, or time limit with:

//Set the memory limit higher
ini_set('memory_limit',$bigValue);
//Set unlimited time limit
set_time_limit(0);

but this does not resove the underlying problem of the resource consuming script.

Related posts:

  1. Running multiple processes in PHP

3 Responses to “Common php errors and how to debug them”

  1. iTechRoom says:

    Must know for all the PHP coder to save debugging time and avoid irritation.

  2. Mayloalfuff says:

    buy synthroid 100mcg
    buy synthroid 25mcg
    http://synthroidmallhere.com/?p=c buy cheap synthroid
    http://i.imgur.com/lNqCpEB.jpg
    synthroid online
    cheap synthroid
    buy synthroid
    generic synthroid
    synthroid without prescription
    buy generic synthroid
    buy synthroid online
    synthroid online without prescription
    cheap synthroid online
    buy cheap synthroid
    cheap generic synthroid
    synthroid 25mcg
    order synthroid
    buy synthroid 25mcg
    cheap synthroid 25mcg
    synthroid 50mcg
    buy synthroid 50mcg
    cheap synthroid 50mcg
    synthroid 100mcg
    buy synthroid 100mcg
    cheap synthroid 100mcg
    synthroid 200mcg
    buy synthroid 200mcg
    cheap synthroid 200mcg

Leave a Reply