A Tip for the PHP Noobs: Get the Description of the Error or Function Causing the Error

I am a self thought PHP coder and when I was new to this fantastic language I always wanted to know why errors are errors and why certain functions are causing errors. Also I was clueless of why errors are (sometimes) hyplerlinked?

Likewise, every PHP newbie would ask the same frustrating question and would want a description of the mystery behind the error.

Here, I will show you a quick tip to turn your test environment into a study center. Follow the steps below and enjoy your time with PHP.

Hint: I assume you are using XAMPP (the all-in-one suite of Apache, mySQL and PHP) running on Windows machine. You can still implement this technique in any PHP installation though.

Preparation

Download and install XAMPP on your computer.

Visit the PHP manual download page, and select one of the downloads under the ‘Many HTML files’ column. There are many languages to choose from but the English version is the most accurate.

Extract the downloaded archive. Inside the extracted archive you should find a folder named ‘html’. Copy that folder —and its contents— into the following directory:


C:\<your-xampp-installation-path>\

After successfully copying the folder into the XAMPP installation directory, rename your copied folder to ‘phpmanual’. It should be something like this:


C:\<your-xampp-installation-path>\phpmanual

Installation

Well, we placed the PHP manual above the DocumentRoot, just in an out-of-web-reach directory, the reason is that there may be multiple domains or hostnames maintained on the machine via VirtualHost containers, so we need to make the manual work on every domain or hostname on the machine.

Open the XAMPP configuration file:


C:\<your-xampp-installation-path>\apache\conf\extra\httpd-xampp.conf

…then insert these lines in it:


Alias /phpmanual "C:/<your-xampp-installation-path>/phpmanual/"
<Directory "C:/<your-xampp-installation-path>/phpmanual">
    AllowOverride AuthConfig
    Order allow,deny
    Allow from all
</Directory>

Just make sure to change C:/<your-xampp-installation-path> to your XAMPP installation drive and path, respectively.

Configuration

Next, open your PHP.INI file found at: C:\<your-xampp-installation-path>\PHP\php.ini and find the following line:

;html_errors = Off

Uncomment the above directive, then turn it on like this:

html_errors = On

Find these two lines:


;docref_root = "/phpmanual/"
;docref_ext = .html

Uncomment the above two directives, they should match the code below:


docref_root = "/phpmanual/"
docref_ext = .html

Save your changes and close the file.

Restart Apache.

Testing

Now, lets see if our effort is working properly. Create a PHP file with the following code inside it:


<?php

include 'a-php-file-that-does-not-exist.php';

?>

Save the file as test-manual.php in C:\<your-xampp-installation-path>\htdocs\, then visit the following URL in your browser:


http://localhost/test-manual.php

If everything was correct, you should receive an error like the following one:


Warning: include(a-php-file-that-does-not-exist.php) [function.include.html]: failed to open stream: No such file or directory in C:\web-server\htdocs\localhost\test-manual.php on line 3

Warning: include() [function.include.html]: Failed opening 'a-php-file-that-does-not-exist.php' for inclusion (include_path='.;C:\web-server\php\pear\') in C:\web-server\htdocs\localhost\test-manual.php on line 3

function.include.html should be hyperlinked and when you click it you should see a page describing the error, otherwise, there is something wrong with your configuration.

Warning

Never use this feature for production boxes. This feature was intended to support your development since it makes it easy to look-up an error or function description. However it should never be used on live websites (e.g. systems connected to the Internet).

2 comments

  1. Ryan says:

    That’s great, I’ll be using it on my local server.
    And you can use it with any PHP installation, as it just requires editing php.ini which every PHP installation has.

    I don’t have XAMPP, as I have a Mac, and I just have Apache with PHP and MySQL installed on my machine.

  2. Jamal says:

    Ryan, yes. You can use it with any PHP installation but you’ll need to edit your httpd.conf to add the alias I added in httpd-xampp.conf.

    And by the way, there is a XAMPP version for Mac 😉

Comments are closed.