Common php errors and their fixes
A very large percentage of errors are actually quite simple to spot and solve once you are sufficiently experienced.Some of the mistakes are very basic, but trip up even the best PHP programmer. This article highlights common php errors that developers need to beware of.so that you can write error free and clean code . The main think needs to remember while you are doing code is always turn on your error display with
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
However, this doesn’t make PHP to show parse errors – the only way to show those errors is to modify your php.ini with this line.
display_errors = on
1)Memory Exhausted Error: This error will display if any of your function or script is eating your memory . you’ve encountered this error when you see this error message.
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 2348617 bytes) in /home4/xxx/public_html/check.php on line xxx
Solution The error can be resolved with a simple trick
define ('wp_memory_limit', '64M');
2)Warning: Cannot modify header information:The page/output always follows the headers.Functions that send/modify HTTP headers must be invoked before any output is made. Otherwise the call fails and you will see the following warning screen.
Warning: Cannot modify header information – headers already sent by (output started at /path/check.php:125) in /path/check2.php on line 15
why headers must be sent before output:
To understand why headers must be sent before output it’s necessary to look at a typical HTTP response. PHP scripts mainly generate HTML content, but also pass a set of HTTP/CGI headers to the webserver:
HTTP/1.1 200 OK Powered-By: PHP/5.3.7 Vary: Accept-Encoding Content-Type: text/html; charset=utf-8 <html><head><title>page titlw</title></head> <body><h1>page content </h1> <p>page output by php code blaaaaah blaaaaaaaaaah...</p> and <a href="/"> <img src=internal-icon-delayed> </a>
The page/output always follows the headers. PHP has to pass the headers to the webserver first. It can only do that once. After the double linebreak it can nevermore amend them.
When PHP receives the first output (print, echo, ) it will flush all collected headers. Afterwards it can send all the output it wants. But sending further HTTP headers is impossible then.
1) Whitespace before blocks.
3) Whitespace after ?>
4) Preceding error messages
If another PHP statement or expression causes a warning message or notice being printeded out, that also counts as premature output.
3) Cannot redeclare function: This error screen will visible if you are defining a function second time . may be this function is defined in php library or you define this two time . or you are including file contains this function two time . The error message will look like .
Fatal error: Cannot redeclare functiondemo() (previously declared in /home/u805727311/public_html/wp-includes/functions.php:1684) in /home/u805727311/public_html/path/includes/xyz.php on line XXX
4) Internal Server ErrorThis is the most common error message . The most likely issue is a corrupted .htaccess file. So first of all check your .htaccess file . secondly try to increaso your php memory limit with .
define ('wp_memory_limit', '64M');
5)Call to undefined function:This error will display if you are calling an undefined function or calling a defined function with wrong name . I.e if you are calling is_array() as isarray() then php will throw a fatal error that function is not defined . The error message is
[01-Jun-2011 14:25:15] PHP Fatal error: Call to undefined function myfunction() in /home/accountname/public_html/your user/file/index.php on line 7
6)Unexpected string:If you are receiving an error which says ‘parse error: unexpected’ this usually means that you have forgotten to include a character. The most common are:
Unexpected ‘=’ : you have forgotten to include the $ when referencing a variable
Unexpected ‘)’ : you have forgotten to include the opening bracket (
Unexpected ‘(‘ : you have forgotten to include the closing bracket )
Unepxpected T_STRING: you have forgotten a quotation mark or a semi-colon at the end of the previous line
Unexpected T_ELSE: you have an else statement with no opening if statement
7)Maximum execution time exceeded : You may receive a message such as “Maximum execution time of 30 seconds exceeded” or “Maximum execution time of 60 seconds exceeded”. This means that it is taking to longer for a process to complete and it is timing out. There are a number of ways to fix this error.solution to this error is either set the execution time at run time with ini_set() or edit your php.ini file to set this
8)Use of an undefined constant:As with parse errors, “use of an undefined constant” means that you are missing a character. It could be one of the following:
Missing a $ when referencing a viariable
Missing quotation marks around array keys
9)only variable should pass by reference:Assign the result of explode to a variable and pass that variable to end:
$file_extension = end(explode('.', $file_name));
if you are using the above code it will throw this error .The problem is, that end requires a reference, because it modifies the internal representation of the array (i.e. it makes the current element pointer point to the last element).
The result of explode(‘.’, $file_name) cannot be turned into a reference. This is a restriction in the PHP language, that probably exists for simplicity reasons.
$tmp = explode('.', $file_name); $file_extension = end($tmp);
10)Undefined index:If you are getting an element from array in which the called index does’t exist then that is an error .The error looks like this
Undefined variable: user_location in C:\wamp\www\mypath\index.php on line 12
Solution for this problem is
Recommended: Declare your variables. Or use isset() to check if they are declared before referencing them, as in:
$value = isset($_POST['value']) ? $_POST['value'] : '';.
Use empty(), so no warning is generated if the variable does not exist. It’s equivalent to !isset($var) || $var == false. E.g.
echo "Hello " . (!empty($user) ? $user : "");
More from my site
Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request