PHP: Dealing With Stuff That Doesn’t Exist

Introduction

As the application you’re coding becomes more and more complex, there’s more of an opportunity for things to go wrong in all sorts of unforseen ways.  One of these problems is variables or functions that don’t exist.  The problem is when you code in such a way that you presuppose their existence. But their existence isn’t guaranteed!

A simple case of this happening is when you’re dealing with user input.  As it turns out, the user didn’t fill out a text field, but the variable for that text field still gets passed around as if it did exist.  More specifically, the variable gets set, but it gets set to an empty string (”).

Check that the variable isn’t empty

A common way of dealing with this is to check that the variable isn’t empty before using it:

$name = 'Joe';
if($name != '') {
	echo 'Hello ' . $name;
}

This turns out to be ALMOST equivalent to the following, which uses empty():

$name = 'Joe';
if(!empty($name)) {  // make sure $name isn't empty
    echo 'Hello ' . $name;
}

However, as Zachary Johnson points out, empty($variable) evaluates to true when $variable=’0′, so you may want to avoid using this.

Check that the variable is defined

There’s another case we haven’t tried yet: what happens if we haven’t defined the variable?

error_reporting(E_ALL);
if($name != '') {
	echo 'Hello' . $name;
}

Nothing catastrophic happens, but if we turn on error reporting, we can see that PHP is a bit upset. We get a notice that the variable $name is undefined, yet we’re trying to evaluate it. The value hasn’t been set yet. And there’s a function to check for that:

$name = 'Joe';
if(isset($name)) {
	echo 'Variable is set';
}

We can combine what we’ve learned into the following:

$name = 'Joe';
if(isset($name) && $name != '') {
	echo 'Hello ' . $name;
}

There’s one neat thing here – if $name isn’t set, it evaluates the first part of the If condition as false (isset($name)) and doesn’t even look at the second part of the If condition ($name != ”). This is because it doesn’t matter what comes after, it will still evaluate to false (false && true evaluates to false, and false && false evaluates to false). Looking at the rest of the If condition is a waste of CPU cycles, so PHP doesn’t do it!

We can confirm this with the following code, which evaluates to false on the first If condition, then doesn’t even try to evaluate the call to a nonexisting function, and thus gives no errors:

if(false && function_that_doesnt_exist()) {
	// do stuff here
}

Check if a function exists

For functions there’s a similar helper called function_exists():

function my_function() { }
if(function_exists('my_function')) {
	my_function();
}

And it doesn’t matter if your function is declared before or after you call it! Check it out – this works fine:

if(function_exists('my_function')) {
	my_function();
}
function my_function() { }

Check if an object’s method exists

Similarly to checking if functions exist, we can check for the existence of functions belonging to an object (we call these the object’s methods) with method_exists():

// Create a test class
class myClass {
	// Create a test function
	public function my_function() {}
}

// Instantiate the class
$myObject = new myClass();

// Check to see if the test function exists
if(method_exists($myObject, 'my_function')) {
	// Do stuff here
}