My Favorite PHP Helper Function - A Better Isset()
PHP7’s Null Coalesce Operator
As of PHP 7 the function described below is no longer neccessary as it’s been superceded by the Null Coalesce Operator.
Null Coalesce allows a nice bit of syntactical sugar for a checking if a variable is set and then returns that variable if it is or some fallback value if it is not:
Outputs the value of $title
if it is set or ‘Blog Title’ if it is not. It is
the same as doing:
My favorite helper function for CodeIgniter is a ridiculously simple function that has an amazing amount of utility. I stole, at least the idea of, this function from LemonStand and it has since made its way into nearly every CMS that I have worked on:
At first this doesn’t really seem to be doing much, after all at first glance
it looks like it is nothing more than a wrapper for isset,
but this improves
heavily upon isset
in two very important ways. First, let’s look at how this
function works.
In the function definition we are taking a reference to a variable. Recall, a
reference is pointing at the memory, not value, of a variable and so we can
actually pass to our helper function a variable that has not yet been
initialized. This saves us from receiving a notice that the variable does not
exist. Our call to isset thus checks our memory to see if it is actually
referencing a variable or nothing at all. If it is referencing an actual
variable it returns that variable, otherwise it returns null (our default
default) or whatever value has been assigned to $default.
The utility of this is best expressed in handful of examples. The biggest use of this is in a view. Let us look at a view constructed without the use of our helper function:
In a sizable view the above can get quite long and quite cumbersome to
maintain. Each call to isset is checking to see if the controller actually
passed the value on to the view ( $title
or $content
). If we did not do
this we would get a notice
from PHP.
Sometimes this is resolved by
programmers by using the error suppression symbal (@
), however the notices
will still end up in the logs of many frameworks that strictly check for
errors. Contrast this with a view using our helper function:
The above is a much, much more concise view that is easier to read and is still
a strictly valid snippet of PHP
that generates no warnings
or notices.
Once we start to use this helper function regularly all different kinds of uses
come up for it, for example we can use it to see if a model returned a value:
The above snippets are fairly simple, but let’s walk through them. In both
instances we need to pass some page
object on to the render method. An error
occurs if it does not get a valid page
object so we must check after
retrieving a page that it actually exists. In the first snippet we use four
lines of code to first get a page by $url
(the value of which is set
somewhere else). Now if the pages model returns nothing then we enter a
conditional statement that retrieves the 404 error page.
However, with the use of our helper function we can shorten the code in half
and remove the conditional all together make it a much more readable snippet of
code. The first line of the second snippet simply passes the return of the
pages
model and the get404
method into our helper function which returns
the first if it returns something or the latter if it does not. The only
downside is the additional load since the 404 page would also need to be loaded
concurrent to the current page with each request, but in most cases this is
going to be negligible.
Having looked at two different uses for our helper function, we can begin to see that we can get quite a bit out of some very very small functions. If you have your own favorite one-liner functions feel free to share in the comments below.