Archive for 'Error handling'

When a function doesn’t exist

Sometimes we develop PHP scripts that requires other 3rd party libraries or makes use of PHP4 or PHP5 specific functions. What we then forget is that when we upload it to a server with PHP4 on and we developed for PHP5, we’re going to end up with a lot of “function not found” errors.

Yes, PHP4 is old news, and everyone should be using PHP5 by now, but there are still a lot of times I come across the need for PHP4 development.

What I’ve started doing is making PHP4 equivalent functions. But this is not what this post is about, what I want to tell you about is the function_exists function.

if (function_exists(’FUNCTION_CALL’)) {
  FUNCTION_CALL();
}

You can define your own function by doing this:

if (!function_exists(’FUNCTION_CALL’)) {
  function FUNCTION_CALL() {
    echo "this function does something";
  }
}

The above will only create your own function is the function doesn’t already exist.

Error suppression operator

In PHP the error suppression operator is the @ sign. Whenever you put this sign in front of an expression, it doesn’t show up any errors that the expression generates. This is a handy feature if you don’t want errors showing up while the script is running. The problem with this though is that a lot of people are using it incorrectly. It might not always be a problem, but because the suppression operator is rather slow when it comes to performance, it can slow down your script.

So, if performance is an issue, have a look at these examples on how to go about not using the suppression operator but still getting the same affect.

if (isset($albus)) $albert = $albus;
else $albert = NULL;

is the same as

$albert = @$albus;

Another method would be to use the variable as a reference varilable:

$albert =& $albus;