1. PHP intro
  2. PHP basics
  3. PHP variables
  4. PHP functions
  5. PHP conditions
  6. PHP loops
  7. PHP arrays
  8. PHP classes & objects
  9. PHP strings
  10. PHP forms
  11. PHP entities
  12. PHP files
  13. PHP include files
  14. PHP date & time
  15. PHP cookies
  16. PHP databases
  17. PHP sessions
  18. PHP summary

PHP functions

A function is a segment of code grouped into a single entity. The great thing about functions is that you can re-use them. By doing so, you eliminate the need to re-type the same code over and over again. Once you implement a function, you can use its code by calling that function.

This tutorial focuses on:

Creating functions

To create a function, use the function keyword, followed by the function name, followed by parenthesis, followed by an opening brace and a closing brace. The lines of code which make up the function will go in between the braces.

function function_name(){ code for function goes here; }
<?php function printMessage(){ print "Hello, this is a function."; } ?>

In the above example, a function named printMessage is created that will print the text "Hello, this is a function."

Using parameters

Parameters are variables placed inside the parenthesis of a function which are used by the code inside the function in some way. Parameters are separated by commas.

function function_name(param1, param2, etc.){ code for function goes here; }
<?php function printMessage($name){ print "Hello " . $name . "!"; } ?>

In the above example, a function takes a value supplied by its name parameter and prints a message on a webpage.

Calling functions

Once you create a function, how do you actually use it? You have to call the function.

To call a function, refer to the function name and pass the appropriate parameter values to it (if the function has any parameters).

<?php function subtract($num1, $num2){ print $num1 - $num2; } subtract(10, 5); ?>

In the above example, a function named "subtract" is declared and it takes two values supplied by its $num1 and $num2 parameters. The code within the function specifies to print the result of subtracting the second number from the first.

Returning values

Functions can be used to return values. By doing so, the function simply acts as a value after its code has been executed. This way, a variable can take the value of a function which returns a value, or you can just print the value returned by the function.

To return a value from a function, the return keyword is used within the function.

<?php //return the addition of two numbers //supplied by the parameters function add($num1, $num2){ return $num1 + $num2; } //add 2 and 4 and put the value in $additionResult $additionResult = add(2, 4); //print the value of $additionResult print "2 + 4 = " . $additionResult; //skip a line print "<br />"; //print the value returned by passing //the parameters 10 and 10 to the add() function print "10 + 10 = " . add(10, 10); ?>
2 + 4 = 6
10 + 10 = 20

In the above code, the function add() will return the value of the addition of its two parameters. Since this function returns a value, a variable can take its value. The variable $additionResult takes the value returned by the function when its two parameters are 2 and 4. The value of the variable $additionResult will therefore be 6 and it will be printed. The value returned by passing the parameters 10 and 10 to the add() function is also printed.

PHP's built-in functions

PHP has several built-in functions you can use for various purposes.

<?php $a = abs(-5); $highest = max(5, 10, 15, 20); $text = strrev("This is a text string."); $textInUpperCase = strtoupper("here is some text. this is the second sentence of the text."); print $a; print "<br />" . $highest; print "<br />" . $text; print "<br />" . $textInUpperCase; ?>
.gnirts txet a si sihT
© Copyright 2013-2014
Terms of use | Privacy policy | Copyright information