Chapter 5. Functions


To write programs in PHP that contain more than just a couple of pages of code and are still organized enough to be useful, you need to understand functions. Functions provide a way to eliminate repeating the same lines of code over and over in your programs. Functions work by assigning a name to a chunk of code, called a function name. Then you execute the code by calling that name.

There are hundreds of built-in functions in PHP. For example, print_r is a function that prints readable information about a variable in plain English rather than code.

If given a string, integer, or float, the value itself is printed with the print_r function. If given an array, values are shown as keys and elements. A similar format is used for objects. With the advent of PHP 5.0, print_r and var_export show protected and private properties of objects.

Functions run the gamut from aggregate_info to imap_ping through pdf_open_image. Since there are so many, we can only cover some basics in this chapter, but we'll give you enough information that you'll be using functions like a pro in no time at all. You can search http://www.php.net for an exhaustive list of functions.

Specifically, we'll go over the following:

  • How to create a function, give it a name, and execute that function

  • How to send values to a function and use them in the function

  • How to return values from a function and use them in your code

  • How to verify a function exists before you try using it

When to split out code into a function is a bit of a judgment call. Certainly, if you find yourself repeating several lines of code over and over, it makes sense to pull that code into its own function. That will make your code easier to read and also prevent you from having to make a lot of changes if you decide to do something different with that block of code, as it's then only in one spot, not numerous places where you'd have to search and replace to change it.

A function is a block of code that accepts values, processes them, and then performs an action. A function doesn't need to accept values, doesn't have to process anything, and doesn't have to perform an action, other than to return control at the end. Think of making cookies and baking them in oven as a function. You put the raw cookie dough into the oven, which makes the cookie dough the input. The oven bakes the cookie dough; this is the function. The result of the bake function is the edible, baked cookies. The bake function might even take other inputs, such as temperature and bake time. These various inputs are called parameters.

Parameters send information to a function, and then the function executes the code. Functions can use anywhere from zero parameters to a whole list of them. In this example, you'll use the echo function to display some text. echo displays text that you send to it as a parameter. Most functions require you to place their parameters inside of parentheses, but echo is an exception to this rule. Echoing of all variables is nearly foolproof!

Example 5-1 shows about as basic of a program as you can get.

Example 5-1. The ubiquitous Hello world!

 <?php echo ("Hello world!"); ?> 

Figure 5-1 shows how the output of the script appears in a browser.

Figure 5-1. How the echo output looks in the browser window


The echo function simply passes on the "Hello world!" string to the browser once you load the PHP file.

echo is actually a PHP language construct. Practically, this translates to its ability to work without enclosing its parameters in parentheses. It's worthy to note that true functions always require parentheses.


You can use one of PHP's many built-in functions or define your own. We'll talk more about defining other functions later in this chapter.



Learning PHP and MySQL
Learning PHP and MySQL
ISBN: 0596101104
EAN: 2147483647
Year: N/A
Pages: 135

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net