Tag Tips

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).

How to make your WordPress posts dates clickable

Do you know that there’s another way to incorporate links to your blog archive in your site without using the archives by date widget by assigning every date on your blog were clickable! Every bit of every date your site contains, and by assigning every bit of your posts date to a link, and by every bit I mean the three major parts the average date format contains —month, day and year.

I know many of you are already aware about this little trick, but I posted this because many people may need it.

The idea

I was recently designing a new blog theme and I came across an old blog: 1976design.com, the post dates were clickable and were driving reasonable traffic to other parts of the blog. The blog has nothing to do with WordPress and its run by hand crafted blog application written by the owner of the website, but it still inspired me to implement the idea on my theme.

Implementation

Open your theme’s index.php and find the template tag <?php the_time(); ?> then replace that with the following code:


// The month
<a title="<?php the_time('F') ?>" href="<?php bloginfo('url'); ?>/<?php the_time('Y') ?>/<?php the_time('m') ?>"><?php the_time('F') ?></a>
// The date
<a title="<?php the_time('F') ?> <?php the_time('jS') ?>" href="<?php bloginfo('url'); ?>/<?php the_time('Y') ?>/<?php the_time('m') ?>/<?php the_time('j') ?>"><?php the_time('jS') ?></a>,
// The year
<a title="<?php the_time('Y') ?>" href="<?php bloginfo('url'); ?>/<?php the_time('Y') ?>"><?php the_time('Y') ?></a>

The above code will show something like this: May 14th, 2009.

If you think there is a better way to do this or if you have something else to add or to say about this, please share it in the comments section.