Yes, there's nothing quite like awesome, time-saving devices that make lives easier. There are many useful tools out there that can improve your workflow, from CSS grids and layouts to fully fledged content management systems and frameworks.

This tutorial, however, is going to delve into the concept of PHP helper functions, and how you can write generalised snippets for your site that you will be able to use over and over. These are functions that both speed and clean up your coding process, so that repetitive tasks can be minimized, and you can spend more of your time worrying about the important things in life... like dinner.

Hyperlink Helpers

One of the most common repetitive tasks we find ourselves having to write is the hyperlink. By creating a helper function for this we can save time whenever we need to write one, remove any chances of syntax errors, and make sure the code is easy to update if files are moved around, or if you are moving a site to a different domain. To begin, we will make a very simple function in a file called helpers.php:

This little baby can be referred to whenever we need to get the full path to a file, and can be referenced over and over during your site development. This is a more useful method than simply calling the domain with the $_SERVER['HTTP_HOST'] method, because it will not change if someone has typed in the domain with or without the "www", and works happily if your site is on a subdomain, for example "http://localhost/my-site".

It is also incredibly useful when moving the site, because all absolute paths will be referring to this single function, so changing the $domain variable to the new domain will change all of your links sitewide, in one single swoop.

Streamline that Code

What we'll aim to do now is to create a new function called "anchor" that will output a compliant, standard string of html complete with a title. Here is an example of what we'd like to output:

And here is what we'd like to actually be typing:

So here we have a PHP function being referenced which has two variables being passed along: one for the link, and one for the display text. So now, still in our helpers.php file, we can start writing the new function:

Now we can test this out. Create a new file called index.php, in the same folder as this helpers.php file, and type:

Here, we've connected the two files with the require() function, and then called the anchor() function, sending two custom variables with it. If you test this in a browser you will see our desired result:

Make it Flexible

This simple function can be useful, but will no doubt become constricting soon enough. We need to add three new features before it becomes truly awesome and flexible; firstly, an option to add custom title tags; secondly, to be able to add CSS rules; and thirdly, an option for it to open in a new window, if need be.

Let's review custom title tags first. Title tags are very important for SEO purposes, and if you don't put them on your links at the moment, I strongly recommend it becomes a habit from now on. This function will help force you into doing so -- actually, as it's a lot easier to add, and if you haven't added one manually, a default copy of the display text will be used instead.

So here is a modified and improved version of our anchor() function.

Now, if we change our function call in index.php to:

We will end up with this satisfactory output:

Styling and Target Options

To make sure we can still use this helper even if we need to add custom CSS or target tags, we will now add a part to our function that will read a fourth variable, and output the correct html accordingly.

We need this function to be intelligent enough to see if we want to put in a CSS class, a CSS ID, or a target tag, and we also want to be able to put in multiple options, or a singular option, or no options at all. First, let's modify our anchor function a little:

Now we need to make our new function, parse_extras():

This function makes use of the fact that all our extras will start with a unique identifier. If we're passing a CSS ID, it will begin with a hash (#), if we're passing a CSS class, it will start with a period (.), and if we're passing a target, it will start with an underscore (_).

Now that we have created all our necessary functions, we can return to our index.php file and modify our anchor() call.

Here we're passing a CSS ID, and we receive an output of:

If we were to add an array of extras, in case you wanted it to open in a new window and to have a specific ID and class (unlikely, but worth showing off the flexibility!), we'd do it like this:

Here, we pre-establish an array variable called extras, just to keep the code tidy, and then send it along with the anchor() function. This will produce the following output:

Mailto Links

Finally, we'll have a look at how to create mailto links in your applications. We'll make a function called mailto() which will be quite similar to the anchor() function, with one major difference: we will be implementing a cipher on the html output so that the email address can't be crawled by spam-spiders, and will keep your client's email addresses secure.

The method we'll use to hide the email address uses the rather awesome str_rot13() PHP function, which simply takes a string and moves all alphabetical characters thirteen letters along, in the alphabet. If you ever went to boy scouts, you probably remember mucking around with such codes, and deciding you were probably going to be a spy when you grow up.

The cipher will output our code in a garbled fashion, and then, using a JavaScript 'antidote' function, we can pull the characters back thirteen places on the alphabet and restore their original meaning. However, as this function is client-side, the 'antidote' only actions, and the text returns to its normal self, when showed on a screen. Thus, spiders, crawlers and spambots, for the most part, won't have a clue what they're reading.

It's probably worth mentioning that there are many methods of hiding mailto links, some a lot more secure than this, some less so. I like to use this method, and have never seen any spam come through to any of my clients because of it. It's worth mentioning too that the JavaScript method itself was developed by Christoph Burgdorfer, and looks like this:

This takes the garbled string "Njrfbzr frperg fcl pbqr" and shifts all the characters, so that the display on your screen looks like:

However, if you review the source code, which is what the spiders and crawlers will see, you will see no mention of this result -- thus, it remains hidden.

We won't look at the ins and outs of how this JavaScript works, as this is a PHP tutorial, after all. Instead, we'll examine how we can implement this in our new mailto() function.

So let's have a look at what is different here.

We can now use the following syntax in our index.php file to bring up our mailto link:

Or, if we need custom elements, just as before, we can add more variables:

This concludes our first foray into PHP helper functions. This is very much the tip of the iceberg, and, next time, we'll begin reviewing some more complex tools, in the name of form helpers.

Form creation and validation has to be one of my pet peeves when creating them from scratch- there is a lot of repetitive code, painstaking writing, and chances for errors. A series of functions that both output correctly structured forms and check the post data for valid email addresses, passwords, string length, etc, can be a true lifesaver. Ah yes, creating html forms from scratch needs not be so groan-worthy as you previously thought.

If you have any other ideas of what would be really helpful to see in a PHP helper function, be sure to comment your ideas! Michael Watson
Michael J. Watson is a web developer / designer and musician from Manchester UK who runs his own freelance studio called scunby.com. He loves getting his hands dirty in php, playing the violin, CSS, mucking around with jQuery, singing and Photoshop (-ing his girlfriend's face onto dinosaurs).
[null, null, {"document_url": "https://code.tutsplus.com/tutorials/increase-productivity-by-creating-php-helper-functions--net-12715", "unformatted_src": "https://s3.amazonaws.com/nettuts/731_helperFunctions/link-helpers.png", "src": "https://s3.amazonaws.com/nettuts/731_helperFunctions/link-helpers.png", "formatted_filename": "link helpers", "rendered_width": 598, "rendered_height": 342}, null, null, null, null, null, {"document_url": "https://code.tutsplus.com/tutorials/increase-productivity-by-creating-php-helper-functions--net-12715", "unformatted_src": "https://static.tutsplus.com/assets/market/code/php_script-4741ed2bae7195fd7d74053caf84174d.png", "src": "https://static.tutsplus.com/assets/market/code/php_script-4741ed2bae7195fd7d74053caf84174d.png", "formatted_filename": "php script ed bae fd d caf d"}, null, {"document_url": "https://code.tutsplus.com/tutorials/increase-productivity-by-creating-php-helper-functions--net-12715", "unformatted_src": "https://static.tutsplus.com/assets/market/code/javascript-b89862766072bc1ca0cffabf89aae971.png", "src": "https://static.tutsplus.com/assets/market/code/javascript-b89862766072bc1ca0cffabf89aae971.png", "formatted_filename": "javascript b bc ca cffabf aae"}, null, null, {"document_url": "https://code.tutsplus.com/tutorials/increase-productivity-by-creating-php-helper-functions--net-12715", "unformatted_src": "https://static.tutsplus.com/assets/ecosystem/Elements-8d12ebd8ef7a8c6a1fd035134c4011f9.png", "src": "https://static.tutsplus.com/assets/ecosystem/Elements-8d12ebd8ef7a8c6a1fd035134c4011f9.png", "formatted_filename": "Elements d ebd ef a c a fd c f"}, null, {"document_url": "https://code.tutsplus.com/tutorials/increase-productivity-by-creating-php-helper-functions--net-12715", "unformatted_src": "https://static.tutsplus.com/assets/ecosystem/Market-7e6b77007d2f2e3158f3c3279a77a143.png", "src": "https://static.tutsplus.com/assets/ecosystem/Market-7e6b77007d2f2e3158f3c3279a77a143.png", "formatted_filename": "Market e b d f e f c a a"}, null, {"document_url": "https://code.tutsplus.com/tutorials/increase-productivity-by-creating-php-helper-functions--net-12715", "unformatted_src": "https://static.tutsplus.com/assets/ecosystem/Studio-23b8faf49584b9b75c7b8247f7d0b55a.png", "src": "https://static.tutsplus.com/assets/ecosystem/Studio-23b8faf49584b9b75c7b8247f7d0b55a.png", "formatted_filename": "Studio b faf b b c b f d b a"}, null, {"document_url": "https://code.tutsplus.com/tutorials/increase-productivity-by-creating-php-helper-functions--net-12715", "unformatted_src": "https://static.tutsplus.com/assets/ecosystem/Community-e18375bc04b26732cbe441eb1c736937.png", "src": "https://static.tutsplus.com/assets/ecosystem/Community-e18375bc04b26732cbe441eb1c736937.png", "formatted_filename": "Community e bc b cbe eb c"}, null]