PHP Functions

The heart of PHP programming is, arguably, the function. The ability to encapsulate any piece of code in a way that it can be called again and again is invaluable—it is the cornerstone of structured procedural and object oriented programming.

Basic Syntax
Function syntax is, at itsmost basic, very simple. To create a newfunction, we simply use the keyword function, followed by an identifier, a pair of parentheses and braces:

function name() { }

PHP function names are not case-sensitive. As with all identifiers in PHP, the name must consist only of letters (a-z), numbers and the underscore character, and must not start with a number.

Tomake your function do something, simply place the code to be execute between the braces, then call it.

<?php
function hello(){
   echo "Hello World!";
}

hello(); // Displays "Hello World!"
?>

Returning Values
All functions in PHP return a value—even if you don’t explicitly cause themto. Thus, the concept of “void” functions does not really apply to PHP. You can specify the return value of your function by using the return keyword:

<?php
function hello(){
   return "Hello World"; // No output is shown
}
$txt = hello(); // Assigns the return value "Hello World" to $txt
echo hello(); // Displays "Hello World"
?>
Naturally, return also allows you to interrupt the execution of a function and exit it even if you
don’t want to return a value:
<?php
function hello($who){
   echo "Hello $who";
   if ($who == "World") {
      return; // Nothing else in the function will be processed
   }
   echo ", how are you";
}
hello("World"); // Displays "Hello World"
hello("Reader") // Displays "Hello Reader, how are you?"
?>
Note, however, that even if you don’t return a value, PHP will still cause your function to return NULL.

Functions can also be declared so that they return by reference; this allows you to return a variable as the result of the function, instead of a copy (returning a copy is the default for every data type except objects). Typically, this is used for things like resources (like database connections) and when implementing the Factory pattern.

However, there is one caveat: you must return a variable—you cannot return an expression by reference, or use an empty return statement to force a NULL return value:

<?php
function &query($sql){
   $result = mysql_query($sql);
   return $result;
}
// The following is incorrect and will cause PHP to emit a notice when called.
function &getHello(){
   return "Hello World";
}
// This will also cause the warning to be issued when called
function &test(){
   echo 'This is a test';
}
?>

Variable Scope
PHP has three variable scopes: the global scope, function scope, and class scope.
The global scope is, as its name implies, available to all parts of the script; if you declare or assign a value to a variable outside of a function or class, that variable is created in the global scope.

However, any time you enter a function, PHP creates a new scope—a “clean slate” that, by default, contains no variable and that is completely isolated from the global scope. Any variable defined  within a function is no longer available after the function has finished executing. This allows the  use of names which may be in use elsewhere without having to worry about conflicts.

<?php
$a = "Hello World";
function hello(){
   $a = "Hello Reader";
   $b = "How are you";
}

hello();
echo $a; // Will output Hello World
echo $b; // Will emit a warning
?>

There are two ways to access variables in the global scope from inside a function; the first consists of “importing” the variable inside the function’s scope by using the global statement:

<?php
$a = "Hello";
$b = "World";

function hello(){
   global $a, $b;
   echo "$a $b";
}

hello(); // Displays "Hello World"
?>

You will notice that global takes a comma-separated list of variables to import— naturally, you can havemultiple global statements inside the same function.

Many developers feel that the use of global introduces an element of confusion into their code, and that “connecting” a function’s scope with the global scope can easily be a source of problems. They prefer, instead, to use the $GLOBALS superglobal array, which contains all the variables in the global scope:

<?php
$a = "Hello";
$b = "World";
function hello(){
   echo $GLOBALS['a'] .' '. $GLOBALS['b'];
}
hello(); // Displays "Hello World"
?>

Passing Arguments
Arguments allow you to inject an arbitrary number of values into a function in order to influence its behaviour:

<?php
function hello($who){
   echo "Hello $who";
}

hello("World");
/* Here we pass in the value, "World", and the function displays "Hello World"*/
?>

You can define any number of arguments and, in fact, you can pass an arbitrary number of arguments to a function, regardless of how many you specified in its declaration.

PHP will not complain unless you provide fewer arguments than you declared.
Additionally, you can make arguments optional by giving them a default value.
Optional arguments must be right-most in the list and can only take simple values—expressions are not allowed:

<?php
function hello($who = "World")
{
echo "Hello $who";
}
hello();
/* This time we pass in no argument and $who is assigned "World" by default. */
?>

Variable-length Argument Lists
A common mistake when declaring a function is to write the following:

function f ($optional = "null", $required){

}

This does not cause any errors to be emitted, but it also makes no sense whatsoever— because you will never be able to omit the first parameter ($optional) if you want to specify the second, and you can’t omit the second because PHP will emit a warning.
In this case, what you really want is variable-length argument lists—that is, the ability to create a function that accepts a variable number of arguments, depending on the circumstance. A typical example of this behaviour is exhibited by the printf() family of functions.
PHP provides three built-in functions to handle variable-length argument lists:

func_num_args(), func_get_arg() and func_get_args(). 
Here’s an example of how they’re used:

<?php
function hello(){
   if(func_num_args() > 0) {
      $arg = func_get_arg(0); // The first argument is at position 0
      echo "Hello $arg";
   }
   else{
      echo "Hello World";
   }
}

hello("Reader"); // Displays "Hello Reader"
hello(); // Displays "Hello World"
?>

You can use variable-length argument lists even if you do specify arguments in the function header. However, this won’t affect the way the variable-length argument list functions behave—for example, func_num_args() will still return the total number of arguments passed to your function, both declared and anonymous.

<?php
function countAll($arg1){
   if(func_num_args() == 0){
      die("You need to specify at least one argument");
   }
   else{
      $args = func_get_args(); // Returns an array of arguments
      // Remove the defined argument from the beginning
      array_shift($args);
      $count = strlen($arg1);
      foreach($args as $arg){
         $count += strlen($arg);
      }
   }
   return $count;
}

echo countAll("foo", "bar", "baz"); // Displays '9'
?>

Passing Arguments by Reference
Function arguments can also be passed by reference, as opposed to the traditional by-value method, by prefixing them with the by-reference operator &. This allows your function to affect external variables:

<?php
function countAll(&$count){
   if(func_num_args() == 0){
      die("You need to specify at least one argument");
   }
   else{
      $args = func_get_args(); // Returns an array of arguments
      // Remove the defined argument from the beginning
      array_shift($args);
      foreach($args as $arg) {
         $count += strlen($arg);
      }
   }
}

$count = 0;
countAll($count, "foo", "bar", "baz"); // $count now equals 9
?>

Unlike PHP 4, PHP 5 allows default values to be specified for parameters even when they are declared as by-reference:

<?php
function cmdExists($cmd, &$output = null) {
   $output = 'whereis $cmd';
   if(strpos($output, DIRECTORY_SEPARATOR) !== false) {
      return true;
   }
   else{
      return false;
   }
}
?>

In the example above, the $output parameter is completely optional—if a variable is not passed in, a new one will be created within the context of cmdExists() and, of course, destroyed when the function returns.

Next Topic | Previous Topic

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail

Leave a Comment

Your email address will not be published. Required fields are marked *