Functions in PHP
A complete PHP application
might contain upwards of dozens of individual script files, with hundreds
of lines of code in each. Between all of those, there will be many instances
where the same piece of code is reused: something as simple as outputting
a string to the browser or as complex as making sure a user is logged
in across multiple pages, and prompting them for login information if
they are not.
As you might imagine, copying the same code you need to every location you need it in is neither practical nor efficient.
Fortunately PHP, like most other programming languages, includes a language structure called a Function. A Function is a self-contained code block that performs a specific job. In most cases, they accept input from the main script as arguments, and return some kind of result. They can help modularize a script by allowing the programmer to break the script down by task. They are also portable, so if you write a useful function for one script, you can easily reuse it in another.
In this article, we will be covering some introductory information on functions. We will also be taking a brief look at PHP's pre-defined functions before focusing on creating and using your own.
One of things that makes PHP so easy to use is the wealth of built in functions. These functions provide the tools to easily complete many of the everyday tasks you need to perform in the course of writing a full-fledged application. There are functions for working with just about everything in PHP: strings, arrays, databases, dates and times, email, numbers, even functions for working with other functions.
With so many out there, it would be impossible to cover all of PHP's functions in a single article. However, as the series progresses, you will be introduced to many of the more useful ones. You can also find the complete list of PHP's built in functions at the their site.
Right now, let's take a look at a couple of these functions, just to explore how they can be used.
Though you have probably used it before without thinking about it, print(); is actually a built in PHP function. Basically, print accepts a string as an argument and outputs it. It's a little unique because it works with or without parentheses:
print "This is a test<br>\n";
print ("This is a test<br>\n");
include(), which allows you to include one file inside of another, is one of those functions that you will find yourself using all the time. If you have ever seen web sites using php where the links to other pages within the site look like http://www.domain.com/?page=pagename or http://www.domain.com/?pagename, it is likely that they are using this function to dynamically generate their web pages from included files.
To see how it works, first create a file called test.txt and write some text in it. Next, create a new PHP file called test.php and place the following inside of it. Just like print(), the parentheses are optional:
Make sure both are saved to the same directory on your web server. When you run test.php, you will see that the text you placed in text.txt was outputted to the browser.
You can easily include text, html or php files in this manner.
This function allows you to send a raw HTTP header. Most of its applications are technical, but one of the most common uses is something relatively simple: redirecting the user. This has the same effect as the META redirection tag in HTML: