PHP Syntax Debugging Reference Information

The following information about common PHP Notices, Warnings, Parse and Syntax Errors may appear when you are writing PHP scripts. We are listing these here in case you want to see a list of the more common problems people have, and how to fix them. I will update this list over time as I get time. I hope it helps in some instances.

Fatal Error: Allowed memory size of X bytes exhausted
Fatal Error: Allowed memory size of X bytes exhausted
This error renders when a script exceeds the default "memory_limit" setting inside your PHP configuration file(php.ini) on your server. Best practice is to always make more efficient programs instead of maxing out the PHP memory limited allocated. If you must allow a beefy memory hog file to run you can check the fix suggestions below. You may have to restart Apache to have any php.ini changes take place, especially on local PHP environments.
Suggestions for fixing
Adjust your php.ini file "memory_limit" setting to something higher using this line:

memory_limit = 20M

Or use the ini_set() php function directly in the script that is exceeding default memory limits. Example:
<?php
ini_set
("memory_limit","20M");
?> 

Fatal Error: Call to undefined function
Fatal Error: Call to undefined function
This error will render when you call a function to run that does not exist to the script, or you are not using the proper case when calling the function.

When working with classes in PHP it often renders when you forget to place "$this->" when refering to on object.
Suggestions for fixing
Be sure to only call functions by name that exist, and be sure to use the proper casing. One can use the "function_exists('sayHi')" php function in a condition statement before attempting to run any function. That function was made to see if functions exist in the script.
<?php 
function sayHi(){ 
    echo 
"hi";
}
if (
function_exists("sayHi")) { 
    
sayHi();

?>


Or just make sure you call the correct function name

<?php 
function sayHi(){ 
    echo 
"hi";
}
sayhi();// Wrong
sayHi();// Right
?>

Fatal Error: Call to undefined method
Fatal Error: Call to undefined method
This error usually renders when a coder working with PHP classes forgets to define an object correctly in their calling script, then attempts to access that object's methods. It can also render when you forget to place "$this->" when refering to on object and its methods.
Suggestions for fixing
Use the "method_exists()" php function it will check to see if a method exists or not in your script before you try to use it, thus defeating this Fatal Error. However it is best to learn how to properly define object instances and communicate through them to defeat this error most efficiently, and not require a check like the one below.
<?php
if (method_exists($myObjectInstance,"method")) {

    
echo "methodName is available for use";

} else {

    
echo "methodName is NOT available for use";


?>


Fatal Error: Cannot redeclare
Fatal Error: Cannot redeclare
This error usually means that you are trying to declare a function or class that has already been declared in your script.
Suggestions for fixing
Do not declare things twice, like functions, classes, etc...
<?php
//----------------------------------------
// Let us say you have this function
//----------------------------------------
function sayHi(){ 
    echo 
"hi";
}
// That same function cannot be redeclared
function sayHi(){ 
    echo 
"Goodbye";
}
//----------------------------------------
// Same goes for classes
//----------------------------------------
class sayHi{}
// Do not declare any class twice
class sayHi{}
?> 

Fatal error: Out of memory
Fatal Error: Out of memory
This error renders when a script exceeds the default "memory_limit" setting inside your PHP configuration file(php.ini) on your server. Best practice is to always make more efficient scripts instead of maxing out the PHP memory limited allocated. If you must allow a beefy memory hog file to run you can check the fix suggestions below. You may have to restart Apache to have any php.ini changes take place, especially on local PHP environments.
Suggestions for fixing
Adjust your php.ini file "memory_limit" setting to something higher using this line:

memory_limit = 50M

Or use the ini_set() php function directly in the script that is exceeding default memory limits. Example:
<?php
ini_set
("memory_limit","50M");
?> 

Notice: Undefined Index:
Notice: Undefined Index:
This notice means that a global variable or database table field has not been set in your PHP before you attempted to script with it. Meaning you have to see first if that variable is set or ready for use in the script. If you are building a form all you have to do is see that one of the form's $_POST variables isset() before gathering the variables up into local PHP variables. Simply trying to place a $_POST[''] type variable in your script without seeing if it is set first, will render this Notice in many PHP environments. Some environments will not render this error depending on the PHP configuration on the server.
Suggestions for fixing
Before trying to access certain variables or globals make sure that one in the group is set first... then script your heart out using those variables. Use the isset() PHP function.
<?php
if (isset($_POST["submit"])) { 

    
$button $_POST["submit"]; 
    
$uname $_POST["name"]; 
    
$uemail $_POST["email"];

}
?> 


FOR MySQL QUERY SPECIFIC ISSSUES: If you have a MySQL query in place and you are trying to access fields that you did not specify from the database, you will get this notice. For instance if you query "SELECT id FROM myTable ORDER BY itemDate DESC LIMIT 1", you cannot expect to have access to any other table field than "id". Try "SELECT * "(select all fields) to get access to all the fields in that table.

Notice: Undefined variable:
Notice: Undefined variable:
This notice means that a variable that is in place for output or manipulation in your script has not been initialized or defined prior to using it.
Suggestions for fixing
At the top of your PHP script you can simply initialize the variable names with an empty value, or wherever it makes sense to define them. Most times on top is best. After it is initialized the notice will go away and you can use those variables for output or anything else.
<?php

$var1 
""// initialize blank or place any default value upon creation
$var2 ""// initialize blank or place any default value upon creation

?>

Parse Error: syntax error, unexpected $end
Parse Error: syntax error, unexpected $end
When you see the "unexpected $end" syntax error it is telling you that you left something open that should have closed(usually improper curly bracket or quote nesting). This error is many times difficult to pinpoint in your script because the error line that is returned is not going to be the line where the nest opens. The error line is usually where the program ends. Functions, loops, condition statements, as well as other mechanisms use curly brackets to open and close those mechanisms ( { } ). Strings will use quote marks for encapsulation and nesting. This error will also render when you use short opening PHP tags(<?) instead of the normal(<?php). To use short PHP tags you must configure your php.ini file to make them work.
Suggestions for fixing

1. Be sure that all mechanisms that use a set of curly brackets ( { } ) to nest things, are properly nested. When each curly bracket in your script opens, it has to close also.

2. Be sure all of your strings are quoted correctly. For example:
$name = "Adam; // will render unexpected $end
$name = "Adam'; // will render unexpected $end
$name = "Adam"; // is correct quote mark nesting.

3. Use normal full PHP opening tags(<?php) or else set your php.ini configuration file to use the short PHP opening tags(<?) using the following line in your php.ini file:
short_open_tag = On

 

Parse Error: syntax error, unexpected T_LNUMBER, expecting T_STRING
Parse Error: syntax error, unexpected T_LNUMBER, expecting T_STRING
This notice usually renders when you begin the name of a function with a number.
Suggestions for fixing
Do not use a number as the first character in the name of function, object, or variable. This function name below will render the error.
<?php

function 2ndFunction() {
    echo 
"The number as first character in the name of the function will render this error.";
}

?> 

Parse Error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE
Parse Error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE
This notice will render when you begin the name of a variable with a number.
Suggestions for fixing
Do not use a number as the first character in the name of any variable, object, or function.
<?php
$2var = "Hello"// Wrong
$var2 "Hello"// Right
?> 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
This warning will usually appear when the fields you are claiming in your SQL syntax do not exist in your database table.
Suggestions for fixing
Be sure your SQL syntax regarding field names are an exact match to the field names in your database table.
<?php
// id, title, and description must all be proper spelling and 
// casing in relation to fields in your database
$sql mysql_query("SELECT id, title, description FROM myTable");
$numRows mysql_num_rows($sql); // Will render this warning if any fields are off
?>

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
This warning will usually appear when the fields you are claiming in your SQL syntax do not exist in your database table.
Suggestions for fixing
Be sure your SQL syntax regarding field names are an exact match to the field names in your database table.
<?php
// id, title, and description must all be proper spelling and 
// casing in relation to fields in your database
$sql mysql_query("SELECT id, title, description FROM myTable");
while(
$row mysql_fetch_array($sql)){ 
    
$id $row["id"];
    
$title $row["title"];
    
$description $row["description"];
    echo 
"$title<br />";
}
// mysql_fetch_array() will render this warning if any fields are off in your SQL syntax
?>

Warning: mysqli_error() expects exactly 1 parameter, 0 given
Warning: mysqli_error() expects exactly 1 parameter, 0 given
This warning will appear when you do not place a parameter in the mysqli_error() function. It is looking for a link identifier returned by either mysqli_connect() or mysqli_init().
Suggestions for fixing
When you use the mysqli_error() function you must place your mysqli_connect variable or mysqli_init variable inside it.
<?php  
// Make the connection to database here   
$myConnection mysqli_connect("$db_host","$db_username","$db_pass""$db_name"
or die(
"Incorrect data"); 
$sqlCommand "SELECT * FROM myTable";<br />
// When you use the mysqli_error() function you must place your connection variable in it 
$query mysqli_query($myConnection$sqlCommand) or die(mysqli_error($myConnection));
?>

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax
You have an error in your SQL syntax;
SQL syntax is separate from PHP syntax. It is the string of text used inside of your mysql_query() functions. The many ways you can cause errors in your SQL syntax cannot reasonably be listed here. Best bet it to learn raw SQL syntax apart from how it applies to PHP, and then learn the many ways PHP communicates to your MySQL database.
Suggestions for fixing
Be 100% sure that your syntax is correct for the query. That error message would go away if you make the syntax correct.
 


This PHP syntax debugging information is currently being maintained by Adam Khoury in an effort to help php programmers get extended information about common errors, and information normal error output cannot give you. Usually a beginner will not know what some error messages mean or have the experience to know how to fix some errors that appear when building an application.
PHP Programming Library  |  PHP + MySQL Programming Library  |  PHP + MySQL Programming Video Tutorials For Application Production

©2012 DevelopPHP.com