November 24, 2014
Hot Topics:

Functions in PHP

  • January 17, 2002
  • By Elizabeth Fulghum
  • Send Email »
  • More Articles »
User-Defined Functions

As invaluable as PHP's built in functions are, the real power and flexibility comes from being able to write and utilize your own. Imagine you wanted to do something very simple, like print out a greeting. There is no real reason why you would actually need to use a function for something like this, but for the purposes of the next few examples we will. Take a look at the following:

<?php
function say_hi() {
print "Hi there!";
}
?>


At this point, nothing is outputted to the browser because we are only defining what the function is called, and what it will do.

Let's take a closer look at what exactly this code means.

The first line begins with the word "function", which tells PHP that we are starting to write a function. "Say_hi" is the name of the function. Just like variable names, function names must be one word, and can only contain letters, numbers, and underscores. They cannot begin with a number and are case sensitive. The name is followed by an opening and closing set of parentheses, which can house optional arguments, and an opening bracket that denotes where the code within the function begins.

Unlike Control Structures, the opening and closing brackets on functions are required even if you only have one line of code between them.

To actually execute this function, it needs to be called, just like PHP's predefined functions:

<?php
function say_hi() {
print "Hi there!";
}

say_hi();
?>


Keep in mind that since version 4 of PHP, it has not been necessary to declare a function before calling it.


Returning Values from a Function

In the last snippet, text was ouputted directly from inside the function using print(), but what if you just wanted to return a value without actually outputting it? Let's take a look at our example again:

<?php
function say_hi() {
return "Hi there!";
}

$var = say_hi();
print $var;
print say_hi();
?>


Notice how the line within the function has changed: "Hi there" is now preceded by the return statement. When you call a function that uses the return statement, rather than executing and immediately outputting whatever was in the function, the value following "return" is treated as the result of the function. This resulting value can be a string, integer, variable or any other PHP data type. When the function is called, the result can be assigned to a variable, or printed out.

PHP technically only allows you to return a single value, but there are ways around this limitation through the use of Arrays, as you will later learn.

Function Arguments

Imagine that we wanted to make our little script a little more personalized, and greet someone by their name. To do this, we want to make the function accept an argument:

<?php
function say_hi($name) {
return "Hi there $name!";
}
?>


In the function definition, we give the argument a variable name. This is the name that the argument will be accessible as within the function. Calling the function is the same as before, except this time we include a string between the parentheses as an argument:

<?php
print say_hi("Billy Joe Bob");
?>


PHP allows you to have as many arguments as you wish. To include more than one, you need to separate them with a comma:

<?php
function say_hi($name, $greeting) {
   return "$greeting $name!";
}

print say_hi("Billy Joe Bob", "Hi there");
?>


In most cases (although not all), you must have a matching number of arguments in the function defination and in the function call.



Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel