A Guide To PHP Error Messages For Designers

Advertisement

PHP is widely available with inexpensive hosting plans, which makes it a popular choice for developers who write software for the Web. From big platforms, such as WordPress, down to small scripts, such as ones to display image galleries or to send forms to email, thousands of script and products are out there written in PHP that can be installed and used even if you don’t know much about PHP yourself.

I have been a PHP developer for 10 years, and my company has developed a content management system, written in PHP, that is intended to be very simple to install and get started with. So, I spend a lot of time working with designers who are installing a PHP script for the first time. If you are installing a script and something goes wrong, PHP can be incredibly infuriating. Until you know what they mean, PHP errors can be baffling. My favorite message is:

Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM

Paamayim Nekudotayim means “double colon” in Hebrew! But double colon is a lot easier to debug than T_PAAMAYIM_NEKUDOTAYIM.

This article is aimed at designers who are not PHP developers but need to install PHP scripts from time to time. Thus, the problems and error messages we will look at here are those you are most likely to encounter when installing scripts, rather than when writing PHP. The tips should help you work through other error messages and should at least help you give clear information to the script’s developer if you need to ask them for assistance.

“All I Get Is A Blank Page!”

Before we can diagnose PHP errors, we need to see some PHP errors. Ideally, you would first be installing the script locally, perhaps using MAMP on a Mac or XAMPP, both of which allow you to turn on error messages easily. You can get PHP to just spit the errors out directly on the screen or log them to a file for viewing.

If, however, you are installing a script directly in your hosting account, the host will likely have disabled the display of all PHP error messages. This is sensible for security, because an error message could give away important information about the server or database, but it isn’t so useful if you are trying to configure a script. If error displays have been turned off, then a fatal error before any output is made to the page would result in a blank page. An error that happens after some HTML has been outputted would result in the markup being “cut off” (stopping at the point where the error occurred). If you “View source” and see a completely blank page or half a page or are getting a “500 internal server error,” then a PHP error is probably the culprit.

Locate the Error Log

On a live server, you should have access to the PHP error log. This is different than the Web server error log, which shows things such as 404 errors when a page or image is not found. The PHP log gives information on errors and warnings in PHP itself. The host should have configured PHP to send errors to a log rather than write them to the screen.

If no error log can be found, then you might be able to configure PHP to maintain one in a location that you specify. To do this, create a directory on your website named errors. Make this directory writable. Then add the code below to the .htaccess file at the root of your website. If you do not have an .htaccess file, create one.

php_flag  log_errors on
php_flag display_errors off
php_value error_log  /your/path/public_html/errors/php_error.log

Then, protect the errors directory so that the file cannot be downloaded from the Web. You might be able to do this in the control panel or just by creating a new .htaccess file, this time in the errors directory.

Order allow,deny
Deny from all

If you now run the script that caused the error, the error message should get added to the php_error.log file. If it doesn’t work, try creating the file yourself first in the errors directory and making it writable.

If you have no luck creating an error log, then open a support ticket with your hosting provider, explaining what you are trying to achieve and inquiring how to do it.

Errors, Notices And Warnings

PHP classifies the messages that go to the error log as either errors, notices or warnings. These have different levels of severity. An error is something broken in your code. It needs fixing and will cause PHP to stop executing.

A warning is essentially PHP saying, “What you’re doing will likely cause a problem, but I’ll try to continue.”

A notice alerts you to something that needs attention in your script. It could be that the version of PHP on your server is newer than the one supported by the script. Things in PHP are deprecated in the same way that elements in the HTML specification get deprecated. When something is deprecated, it means that it will be removed in the future, so the notice lets you know this.

Errors in a purchased or open-source script are most likely an issue with your installation, assuming that your version of PHP meets the requirement. If a script is throwing out warnings that you cannot fix, then you should probably raise them with technical support. You can usually ignore notices, although you should check that the latest version of the script is installed. If a lot of notices are being thrown out, it could indicate a poor-quality script.

If the trick above for turning on error logging worked for you, you can set the level of error reporting in the .htaccess file, too:

php_flag  log_errors on
php_flag display_errors off
php_value error_log  /your/path/public_html/errors/php_error.log
php_value error_reporting 1

The above will add only errors to the error_log.

php_flag  log_errors on
php_flag display_errors off
php_value error_log  /your/path/public_html/errors/php_error.log
php_value error_reporting -1

Setting error_reporting to -1 will log all errors, warnings and notices. If you do this, then you’ll definitely want to set php_flag display_errors to off, so that notices do not get outputted to your website’s pages.

screenshot1

The First Error Is The Important One

If you are displaying errors to the page and hit a PHP error, you could run into a terrifying page full of errors. If you are reviewing errors in a log, refresh the page that is generating the error and then check the log. You might find a long set of errors for the same page; they should all have the same time code, helping you to identify them as a set. This usually happens when the first error is a warning; the script tries to continue running but hits more problems related to the initial warning. Fix the initial warning, and you might find that all of the other issues go away.

Database-Related Errors

Many of the errors people encounter when installing a script have to do with the connection to the database. Usually, when a script needs to make a database connection, you will be asked for four bits of information: the database’s user name and password, the server’s address (which is often, but not always, localhost) and the database’s name. If any of these are incorrect, you won’t be able to connect. Depending on the script, either you will see an ugly string of error messages or, if the developer has handled this gracefully, you will be told that the script was unable to connect to the database.

The database connection issues that I commonly see when providing technical support are as follows:

  • The user has entered localhost instead of an IP address or URL for the database server.
    Localhost means that the database is on the same server as your website. Some hosts have different servers for MySQL and so will give you an address for the server that you need to use when configuring the script.
  • An incorrect user name has been entered.
    One thing to watch is that cPanel prefixes user names that you create with the account’s name. If your account’s name is fred and you create a user named website, then the user name to enter when configuring a script would be fred_website.
  • An incorrect database name has been entered.
    As with user names, cPanel prefixes the database’s name with the account’s name. Check that you are using the prefixed version in your configuration.

Database Permission Problems

Another possible cause of database-related trouble is when all of the connection details are correct, but the database user doesn’t have the permissions needed to do things that the script needs to do. For example, the script might need to create database tables and so would require the database user to have create permissions.

If you created the database user in cPanel, you would also need to select the database and join the user to it, giving the user enough permissions to run the script. For example, if you were installing WordPress, your database user would need to have All Privileges for the database it is attached to.

In cPanel, you can set these in the section named “Add user to database.” Select the user and database, and then on the next screen, click the “All Privileges” checkbox before submitting the form.

screenshot2

PHP Errors

The next set of errors are those generated by PHP because of some problem with the script that you’re installing or because of something that was introduced while you were installing or configuring it.

Fatal Error: Call to Undefined Function

Fatal error: Call to undefined function my_function() in /home/mysite/public_html/test.php on line 2

This means that a PHP function that the current page needs cannot be found. The function might be part of the script itself or built right into PHP. You might see this error when installing a third-party script for the following reasons:

  • If you’re installing a new script, you might not have uploaded all of the files. In my experience, people who use Dreamweaver’s FTP client encounter this issue a lot.
  • If you’re upgrading, you might not have replaced all of the required files. So, an old version that doesn’t contain the function is being used.
  • You might not have added a required PHP include or specified a correct path in a configuration file.
  • A script might be calling a PHP function that is not available on this server. For example, it might be calling imagecreatefromjpeg() when the GD image libraries haven’t been installed.

To solve the error, check carefully that all of the files have been uploaded or replaced and that you have followed the installation instructions. If you cannot resolve it, then contact the script’s developer, explaining what you have done and what you have checked and copying the error message.

If the issue is that something is missing in your PHP installation, then you might need to contact your hosting provider.

Fatal Error: Cannot Redeclare

Fatal error: Cannot redeclare my_function() (previously declared in /home/mysite/public_html/runtime.php:14) in /home/mysite/public_html/runtime.php on line 26

This usually means that you have included a file twice. Check everywhere you have edited and check included files to make sure you haven’t duplicated an include.

Fatal Error: Allowed Memory Size Exhausted

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 17472 bytes) in /home/mysite/public_html/lib/Image.class.php on line 198

You will typically see this error when trying to upload and process an image, although you might see it for any script that requires a lot of memory to run. The amount of memory that your scripts are allowed to use is set by the hosting provider. On cheap shared hosting plans, these limits are often set very low. If you need to use a script to process large images, contact your provider to see whether you can move to a plan with higher limits.

Permission Errors

Warning: move_uploaded_file(/home/mysite/public_html/upload/my_cat.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/mysite/public_html/upload.php on line 49

This warning is an example of a permissions error. The script is trying to upload a file to a directory, but the directory doesn’t have enough permissions for the file to be moved to it. The permissions that PHP needs in order to write to a file or directory depend on how the server is configured and whether you are using a Unix or Windows host. The section on “Permission Scheme3” in the WordPress Codex is relevant even if you are not using WordPress. However, if you get any kind of “Permission denied” error or warning and can’t fix it simply by changing the permissions for the folder, speak with your hosting provider rather than the developer of the script, because your provider will be able to tell you how to do this.

PHP Warning: Include

PHP Warning:  include(foo.php): failed to open stream: No such file or directory in /home/mysite/public_html/test.php on line 2

This warning tells you that an include file (included using PHP’s include syntax) was not found. It is a warning rather than an error, because PHP will continue trying to load the page if it cannot find an include.

In PHP, you can include files using include() or require(). If you use require(), then you are telling PHP that this script is vital to running the website, and so PHP will spit out an error if the file is not found, rather than try to continue.

If you get a warning or error message that a file could not be opened, then check that the file referred to in the error message is there and that your path to it is correct. For example, if you have the file /inc/navigation.php, and the page calling this file is in the directory /about, then you would need to include navigation.php with the following include:

<?php include(../inc/navigation.php); ?>

Omitting the ../ would result in a warning because PHP would not be able to find the file.

In Summary

PHP errors can seem very confusing if you are not a PHP developer, but if you read the error message carefully, you will usually find something that leads you to the root of the problem. Most of the errors I see in technical support have to do with incorrect database connections, missing files or permissions. These are all things that someone with no PHP knowledge can identify and fix themselves, just by learning how to access error messages and becoming familiar with what PHP is saying in them. Being able to debug these kinds of problems will save you a lot of frustration and will free you from having to wait for tech support to get back to you.

If you run into an error when installing a script, remember the following:

  1. Learn how to access PHP errors on your server.
  2. Look at the error log to see what the error actually is.
  3. See if you can identify the source of the problem by looking at the initial error message or warning for the script.
  4. If the error seems to involve permissions or an incorrect database configuration, open a ticket with your hosting provider.
  5. If the script is the problem, then open a ticket with the developer, explaining what you have done and copying information from the error log. You will bring joy to their heart if you provide the actual error message rather than say you saw a blank page!

Further Reading

Have you come across a particularly baffling error when installing a script? Have you come across a hosting provider that requires a different method to get error logs working? Add your suggestions in the comments section.

Image source on front page: Alex E. Proimos7

(al)

Footnotes

  1. 1 http://www.smashingmagazine.com/wp-content/uploads/2011/11/htaccess.png
  2. 2 http://www.smashingmagazine.com/wp-content/uploads/2011/11/privs.png
  3. 3 http://codex.wordpress.org/Changing_File_Permissions#Permission_Scheme_for_WordPress
  4. 4 http://perishablepress.com/press/2008/01/14/advanced-php-error-handling-via-htaccess/
  5. 5 http://codex.wordpress.org/Editing_wp-config.php#Configure_Error_Log
  6. 6 http://digwp.com/2009/07/monitor-php-errors-wordpress/
  7. 7 http://www.flickr.com/photos/34120957@N04/4199675334/
PHP

↑ Back to topShare on Twitter

Rachel Andrew is a web developer, writer and speaker and one of the people behind the content management system, Perch. She is the author of a number of books including The Profitable Side Project Handbook. She writes about business and technology on her own site at rachelandrew.co.uk.

Advertising

Note: Our rating-system has caused errors, so it's disabled at the moment. It will be back the moment the problem has been resolved. We're very sorry. Happy Holidays!

  1. 1

    Allowed Memory Size Exhausted are often due to memory leaks or when working with possibly several pics in the same script.
    One possible way to workaround this is often to force PHP releasing some memory by deleting, with unset(), variables (those that may contain image or heavy objects/arrays for example) as soon as you no longer need them.

    • 2

      I’d also like to add to that, before asking to upgrade, one should contact the host provider and asking if the memory allocated to php scripting can be increased. They often set the default as much lower than the maximum they allow for shared accounts. I’ve often been able to increase it without having to upgrade a client’s entire plan. Upgrading from shared usually leads to VPS, which in many cases means a blank slate that requires configuration best left to a pro server admin, which a lot of web developers and designers are not. Bear in mind that I’m mainly speaking about sites made for clients.

  2. 3

    Nice article but I’m not sure if some designers will even know how to make a directory writable, or know how much access should be given (644, 777, etc.). At least this is true for some deaigners I know. Maybe go into a little more detail.

    • 4

      I agree.
      “This article is aimed at designers who are not PHP developers”
      I’m not sure but this is a fine synthesis to dive into web development and save time.

  3. 5

    Nice article! I have noticed that the latest version of Dreamweaver (5.5, I think) is much better at picking up syntax errors. That has made my web development go much faster, with less frustration.

    Also, if you can get a terminal login on your hosting server, typing “php -l ” will give you a list of any syntax errors. Before the Dreamweaver upgrade I kept a terminal window open all day just for this quick check. Avoids the dreaded blank page.

    Thanks for the article!

  4. 6

    If memory exhustion is an issue then some hosts allow you to put a php.ini file in the root of your website to change their standard settings, this is simply a text file save with the extension .ini, add the line memory_limit = 10M (or your required value) and this will be parsed whilst loading your site.
    Note that some ‘cheap and cheerful’ hosting packages will probably not allow you to do this.

    • 7

      Apparently, at times Google uses old data when “discovering” crawl erorrs.Example:We have a site which was redesigned and uploaded in June 2010.The whole webspace had been cleaned and a sitemap was introduced with the new design.Now, about one year later, Google reports a 404 error which was discovered in April 2010, two months _before_ our redesign.Apart from that, Google “discovers” missing pages (which never ever existed) linked from nowhere with such nonsensical names as: wmbhvftgobb.html phencgatzpugnz.html uulryhoqgsaxj.htmlAll this shows that the crawl error reports of Google Webmaster Tools are currently not to be taken seriously with Google using outdated and cached data.Maybe Google finds competent programmers in the future, who are able to fix these erorrs in GWT.

  5. 8

    Quick note:

    You can almost always increase the maximum memory size in the .htaccess file.

    Surprising, but this has worked for me every time I’ve needed it, even on cheapo hosting.

    Eg:

    # bigger uploads
    php_value post_max_size 300M
    php_value upload_max_filesize 300M

    # longer running time
    php_value max_execution_time 3600
    php_value max_input_time 3600

    # mo memory
    php_value memory_limit 64M

    You can verify the change in settings has taken effect by viewing the output of phpinfo().

  6. 9

    Another way to increase or to change some php settings, is to use the php-function
    ini_set($varname, $newvalue);
    For example you can write
    ini_set(‘memory_limit’,’64M’);

  7. 10

    Great tutorial of php.

  8. 11

    Many thanks..

  9. 12

    “Database Permission Problems : give ALL the permissions.”
    Sorry but this really suck. Just give your database user the permissions he needs, no more.
    It’s like “Having trouble to write an uploaded file ? use `chmod -R 777 /`”

    However, kudos, because the rest of this article is good, and might help newcomers to PHP to debug the main errors.

  10. 13

    Great article. I’ve found this to be the best reference for the various PHP error reporting settings.

  11. 14

    Hi,

    I am totally new to wordpress so I apologies straight away can you help me with this. I have just loaded a wordpress site on my servers and I am faced with this error. Any help would be appreciated.

    Fatal error: require() [function.require]: Failed opening required ‘/home/shropsbi/public_html/wp-includes/wp-load.php’ (include_path=’.:/usr/local/php52/pear’) in /home/shropsbi/public_html/wp-settings.php on line 21

  12. 15

    Themesrefinery

    July 5, 2014 8:15 pm

    We can easily turn on or off wordpress error reporting with just one line code in wp-config.php. Here is the link for code
    http://www.themesrefinery.com/turn-wordpress-error-reporting/

↑ Back to top