PHP Custom Error Handling Continued

Right, so now we have the nice xHTML ready to slot in (you might need to have a look at previous posts in this series). Theoretically everything should work fine, but if you go back to the first part of the series you still have the problem of clearing up the useless data that PHP has outputted before the error, as it will be displayed before the doctype, invalidating the xHTML:

(useless data here)<!DOCTYPE html PUBLIC…

Here’s where the nifty bit comes in: using something called output buffering. I like to think of output buffering as pieces of paper stacked on top of each other. In this case we’ll only be using one piece of paper, but you could potentially have a lot more. By default your PHP scripts will write to a buffer (or piece of paper) and that buffer will be displayed automatically. You can interfere with this process by manually specifying your piece of paper for your script to write on, so that you can screw it up and throw it into the bin if need be. Simply call:


Before your output begins. Now that you have your buffer, you can throw it out as you please with:


As I mentioned you can have more than one buffer (or piece of paper) so let’s have a look at this:


echo ‘This will display. ‘;


echo ‘So will this.’;


echo ‘No chance’;




This will display
So will this.

The only new thing you are seeing with is:


All this does is to send the buffers to be displayed, and disable output buffering again. This can be combined with a custom error handler to display your errors as you intended:

function myErrorHandler ($errno, $errstr, $errfile, $errline, $errcontext) { (1)

switch ($errno) { (2)


ob_clean(); (3)

$page = &new Page(’An Error Has Occured’); (4)


t<h2>An Error Has Occured</h2>

<b>Error:</b> in $errfile on line $errline<br />



$page->addCentreContent($error); (5)




echo $page->returnPage();

ob_end_flush(); (6)

exit();  (7)




  1. Define your custom error handler with “myErrorHandler”, this can be anything you want but you do have to follow the set parameters: “$errno”; what level of error you are handling like “E_USER_ERROR” “E_USER_NOTICE” or whatever, “$errstr”; the message you pass the function at call-time (more on this later), “$errfile”; the file where the error occurs, and “$errline”; the line number.
  2. Instead of making a code soup with if/else conditionals, it is much easier to use the switch. This takes a variable, so that you can compare it one at a time with the “cases” it could be. The “case” part of the switch basically serves as the “if ($variable == $action)” conditional. Though instead of starting with a curly bracket you use the colon, and finish with “break;”.
  3. Clean out the buffer we started before the output code.
  4. Instantiate the object made in Part 2, and build up the xHTML error page.
  5. Send the output to the browser
  6. Exit code execution so that any potential code after the error that outputs data doesn’t appear after the “</html>”

Now that the error handling function is ready, you have to make PHP recognise it as such, so it replaces its basic messages with your custom ones:


Obviously this should be just underneath your error handling function. Now let’s have a look at a concept example:

echo ‘This is some table xHTML';

if ($failedTableQuery) {

trigger_error(‘I am sorry but the data could not be retrieved from the database’,E_USER_ERROR); (1)


echo ‘I am making judgements on the above data’;

  1. Use “trigger_error” to make your errors

As you can see the benefits are two fold: the useless structure and the comments on the data are discarded, and you have a user friendly (well, as friendly as errors come) error. Some caveats: you will have to add further “cases” to your error handler function to deal with other types of php errors, and PHP 5 has different error handling capabilities that you should Google. Hope you found this series interesting, I welcome your comments. I think I’ll have a once over to check all the code works, but it’s nice to cross another thing off the list.


PHP Custom Error Handling

PHP has many built in error reporting levels, sometimes it is better to use these when your application fails on a fundamental level, specifically E_USER_ERROR.

Sometimes instead of handling fatal errors where they happen, I find bailing out into a global error handler more efficient. For instance take this concept example:

// This could be a failed

MySQL update or whatever

if (‘foo’ == ‘foo’) {

die(‘foo really shouldn’t equal foo’);


// This could be displaying the failed update

echo ‘this doesn’t work if foo != foo';

Ok, so far so good, it may be poor man’s error handling, but at least it gets the job done. The user will never see the echo of the string that relies on foo!=foo, or the “failed update”.

It’s a hassle to continually code die() error messages, especially if you want to render the errors with pretty HTML; and there are clear benefits as to why you should do this:

  • Errors fit into the rest of your site, users get worried when look ‘n feel drastically changes, i.e. from nice CSS layout, to black and white critical mass of text.
  • It makes the site look more professional, and less buggy. You could even dress the error up to the point where it doesn’t look like one anymore. How about instead of “Error 404″, you could build:

I am sorry, but the page you were looking for has been moved. Here is a list of search results applying to the page you wanted:
Use the search box below if you want to continue searching.

  • You can create descriptive errors; instead of “Connection to MySQL Failed”, and a collective wtf from the users, you could change it to “I am sorry but an unforeseen error has prevented the information you requested from being retrieved from our database, please email the admin–etc.”
  • Custom error messages hide your code from being displayed, which could potentially leave you vulnerable.

Consider Digital-to-Analogue Converters Harmful

I have just finished my first whitepaper on a topic that I think every person who uses computers, the internet in particular, should be concerned about. This includes the average user who just goes onto the internet to check their mail, or just to chat with friends. On the realisation that the modulation demodulation of data, and not just internet data at that, was harmful I decided to write a brand new heuristic algorithm– “Snying”, to combat the seemingling carelessness nature of Digital to Analogue converters.

Snying runs on patched standard software. All software was hand hex-editted using GCC 0.7.3, Service Pack 3 built on the American toolkit for rovably improving optical drive speed.

Many cyberneticists would agree that, had it not been for Moore’s Law, the visualization of hierarchical databases might never have occurred [16]. In fact, few biologists would disagree with the development of IPv4.

From my studies I have concluded that I am talking BS The PDF was automatically generated by a program that some MIT students, fed up with computer jargon, created to bluff their way into a computing conference. Read the full article by CNN. You can also generate your own BS by visiting the online generator. The funny (or not so funny) thing is that they actually managed to get one of their papers accepted into the World Multi-Conference on Systemics, Cybernetics and Informatics. With a name as long as that, they’re asking for it anyway.


How Did Google Create Analytics?

Google have absorbed another web industry; that of sophisticated web analysis software. As detailed in their press release they acquired Urchin and opened up their analysis software for free under the name of “Google Analytics”.

While it has been integrated into their AdWords service, you can also track your other keywords too:

Do you buy keywords on search engines other than Google? If you do, Google Analytics tracks them – all of your keywords, on every search engine. For both paid search and unpaid search.

You can use your Google Mail account to login to the service, or you can register immediately for a Analytics account. Once you copy and paste some Javascript into the header of your templates – you do use templates, don’t you? – it takes around 12 hours for the stats to get up and running, and an hour for subsequent statistics to be added.

Electric Cars on the Way

Advocates of electric vehicles like say the switch on an electric car was not a problem, since most trips are short anyway. In a field trial, researchers from the Technical University of Munich (TUM) want to find out whether craftsmen and taxi drivers in the Bavarian capital actually easily to electric drive to switch.

To make this not about electric cars available, but convert their internal combustion vehicles to virtual electric cars: 130 taxis and commercial vehicles get a Smartphone that is to simulate the operation of an electric car.

Simulated and real electric car

On the road, the device collects data: it captured the position and driving behavior – so when accelerating, braking or directed. These data are then converted on a virtual electric car: the Smartphone displays the power consumption would be and how it would affect the charge level of the battery. However Munich not only simulates: the data will be also checked on a real electric car.

It is the first time that such a simulation will be carried out, explain the initiators of the project from the Department of automotive engineering of the TUM. “For many taxi and business will probably be a part of electrification of the vehicle fleet, it is not only technically possible, but today it shows economic and ecological advantages,” says an engineer.

Locations for new charging stations

The researchers want to with the project “Virtual electric mobility in the taxi and commercial traffic Munich” (VEM) find out how the routes, taxis and commercial vehicles daily travel, with electric cars can cope with. The aim of the project will be therefore to identify sites for new charging stations.

Currently, the last vehicles are equipped with Smartphones. You will be able to browse through websites if you have a mobile friendly web host. There is a good compilation here here about mobile friendly web hosts. The simulations begin early next year.

What We Offer

STRATEGIC SOLUTIONS WEST offers complete low cost solutions for interconnecting individuals, offices and networks via the latest in high speed digital transmission systems and equipment. As a full service manufactures representative and Network/System Integrator, SSW can provide nationwide turn key solutions and service for LAN/WAN internetworking, remote access, internet access, telecommuting, and test equipment. As an Authorized Sales Representative for Pacific Bell, AT&T and Sprint, SSW provides extensive WAN services plus the experience for integrating LAN and WAN networks. SSW is also an Authorized Sale Agent for Pacific Bell Internet Services and Internex Communications Services offering a broad range of internet access services and speeds. These services include SMTP & POP mail services, web page hosting, web page server farms and domain name registration.

Our Main Products and Services Include:

  • Interconnect Products And Services

Telecommuting, Office to Office Interconnect, Internet Access and Digital Pre-Press using such services as ISDN, XDSL, Frame Relay, and T1 Services

  • LAN Products

Ethernet, Fast Ethernet & Gigabit Hubs & Switches, Stackable, Manageable Hubs, Work Group Hubs, Personal Hubs, Stackable Work Group Hubs, Local Bridges, Work Group Bridges, Mixed Media Converters & Concentrators, Repeaters, Transceivers

  • LAN/WAN Test Equipment

LAN Cable Testers, LAN Analyzers, Data Link Analyzers, Data Link Simulators, ISDN Central Office Simulators, Protocol Analyzers and LAN Interface and Controller Systems.

  • Web Page Design

SSW offers complete support for home page design and implementation.

  • Design, Technical Customer Support

SSW’s technical staff provides complete LAN /WAN network designs and consulting services. SSW is proud of their commitment to the customer for technical support for all products and systems. We take complete system responsibiltiy for all elements of the network we sell to you.