Utility function: perch_get()

Perch has a number of very helpful utility functions. perch_get() is probably the most used one of them. However, it seems some developers who are new to Perch are not sure what the function actually does.

From the documentation:

The perch_get() function does the same job as $_GET in PHP, but has some convenient safeguards and options built in.

PHP

In PHP, there is a predefined variable called $_GET. It is:

An associative array of variables passed to the current script via the URL parameters.

So when the page URL is:

example.com/blog/post.php?slug=my-fancy-unicorn,

the array $_GET has an element with:

i.e. the value of $_GET['slug'] is my-fancy-unicorn.

And when the page URL is

example.com/blog/index.php?category=unicorns&page=3,

the value of $_GET['category'] is unicorns and the value of $_GET['page'] is 3.

So the $_GET array allows us to get the URL parameters and their values. This allows us to build dynamic pages.

For instance, you can set up a single page called post.php for blog post detail view for all your posts. This page can display any blog post you have by simply passing a URL paramter with the post’s slug like so:

post.php?slug=2018-03-07-big-news


What’s the problem?

While $_GET is great, you need to write more code to use it safely.

When you use $_GET['slug'] on the page post.php with the URL /blog/post.php?slug=my-fancy-unicorn, you get the value of slug.

When you use $_GET['slug'] on the same page, without the ?slug= parameter, you get a PHP error. That’s because the $_GET['slug'] is not set.

So instead of simply using:

$slug = $_GET['slug'];

You would test whether it is been set first AND whether it is empty:

if ( isset($_GET['slug']) && $_GET['slug'] != '' ) {
    $slug = $_GET['slug'];
}

And in some cases you may also want to set a default value in case it is not set:

$category = 'some-default-value';

// overwrite default value
if (isset($_GET['category']) && $_GET['category'] != '') {
    $category = $_GET['category'];
}

So it can be a little inconvenient to keep adding these if statements all the time.


perch_get()

The perch_get() utility function makes using URL parameters easier. For a start, it doesn’t throw an error if the URL parameter doesn’t exist.

perch_get('slug') on a page with the URL /blog/post.php?slug=my-fancy-unicorn, returns the value my-fancy-unicorn just like $_GET['slug'].

However, perch_get('slug') on a page with the URL /blog/post.php, returns false instead of throwing an error.

$_GET vs perch_get()

This:

/* === $_GET === */
if (isset($_GET['slug']) && $_GET['slug'] != '') {
    $slug = $_GET['slug'];
}

is the same as this:

/* === perch_get() === */
if (perch_get('slug')) {
    $slug = perch_get('slug');
}

With $_GET the following throws an error if ?slug= is not present:

/* === $_GET - does not work === */
$slug = $_GET['slug'];

if ($slug && $slug != '') {
    perch_blog_post($slug);
}

With perch_get() you write less code and it works even if ?slug= is not present in the URL:

/* === perch_get() - works === */
$slug = perch_get('slug');

if ($slug) {
    perch_blog_post($slug);
}

Defaults

Assigning a default value is another cool feature of perch_get().

Here’s one way to do it with $_GET:

/* === $_GET === */
$category = 'some-default-value';

if (isset($_GET['category']) && $_GET['category'] != '') {
    $category = $_GET['category'];
}

With perch_get() you can assign a default value like so:

/* === perch_get() === */
$category = perch_get('category', 'some-default-category');

So you can really replace the following 5 lines of code:

/* === $_GET === */
$number_of_posts = 9;

if (isset($_GET['count']) && $_GET['count'] != '') {
    $number_of_posts = $_GET['count'];
}

perch_blog_recent_posts($number_of_posts);

with a single line:

/* === perch_get() === */
perch_blog_recent_posts(perch_get('count', 9));
link