CHAPTER 11Using Functions

Redundant code is rarely a good thing. Rewriting code over and over again is not time efficient and looks rather shoddy from a layout point of view. Like any good programming language, PHP alleviates the problem of redundant code in a number of ways; the most commonly used and simple-to-implement way is by using functions.

A function is basically a block of code that performs a given action from the script that has access to it, via includes, code insertions, or other methods. Rather than repeatedly rewrite the same block of code to, say, check if the current user is logged into your site, you can put the code into what is essentially a code wrapper and then call it at your convenience with a simple function call.

Get PHP 5 Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.