PHP
  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.

Syntax:
function function_name(){ code for function goes here; }
Example:
<?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.

Syntax:
function function_name(param1, param2, etc.){ code for function goes here; }
Example:
<?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).

Example:
<?php function subtract($num1, $num2){ print $num1 - $num2; } subtract(10, 5); ?>
Output:
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.

Example:
<?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); ?>
Output:
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.

Example:
<?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; ?>
Output:
5
20
.gnirts txet a si sihT
HERE IS SOME TEXT. THIS IS THE SECOND SENTENCE OF THE TEXT.
© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information