Please note, this is a STATIC archive of website webdevelopmenttutorials.com from 20 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.

Tutorials

Learn web design and programming with our free video and text tutorials.

Web Designer? Design and market your own professional website with easy-to-use tools.

PHP Functions

What is a Function in PHP?

Functions provide a way to group related actions together and to be able to repeatedly execute them without retyping the code.

PHP has over 700 functions that are already built-in to PHP and PHP also allows you to create your own functions.

How to Create PHP Functions

PHP allows you to create your own functions. To create a new function, you must first use the function keyword. After the function keyword, is the name of the function. Function names can include letters, number or the underscore symbol (_) and can only begin with eith a letter or an underscore symbol. The function name is then followed by a set of paranthesis (()) which are followed by a set of curly braces ({}). Within the parenthesis goes the argument(s) of the function and within the curly braces goes the code to be executed when the function is called.

Below is the basic syntax of a function.

<?php
function functionName () {
// code to be executed when the function is called
}
?>

To call a function, you type the name of the function followed a parenthesis.

The PHP code below creates a function which will be named helloWorld, and then calls the function.

<?php
function helloWorld() {
echo 'Hello World!';
}

helloWorld();
?>

The PHP code would output the text "Hello World!" to your web browser.

built-in PHP Functions

PHP has over 700 built-in functions. The full list of built-in PHP functions can be found at https://php.net/quickref.php.

An example of a built-in PHP function is the phpinfo function. phpinfo is a globally accessible function that accepts no arguments and returns no results. What the phpinfo function does is output detailed information about the current PHP installation on your web server.

Copy the PHP script below into a new php file. If you have PHP installed on your computer, you can veiw the file web server document root or you can upload the file to your web server and view the web page that is created.

<?php
phpinfo();
?>

The image below is an example of what the phpinfo function will output.

php tutorial image

Passing Parameters to a Function

You can pass parameters (also called arguments) to all functions that you create and to some built-in PHP functions. A parameter within a function is just like a variable. The parameters are listed within the parenthesis. If there is more than 1 parameter, each parameter is seperated by a comma.

The PHP script below has 1 parameter listed.

<?php
function userName($user) {
return "Hello, $user";
}
echo userName(John);
echo "<br />";
echo userName(Mike);
echo "<br />";
echo userName(Gina);
?>

The PHP script above would output the text "Hello, John" to the screen.

Hello, John
Hello, Mike
Hello, Gina

The PHP script below shows you how to pass multiple parameters.

<?php
function totalNumber($num1, $num2) {
$total = $num1 + $num2;
echo $total;
}
totalNumber(10, 20);
?>

The result that the PHP script above is below.

30

Default Parameters in PHP

PHP allows you to set default values. To do this, add an assignment operator (=) and value to the variable name in the function declaration. If you omit an optional value it must be the last value you pass to a function.

The PHP script shows you how to set a PHP default parameter in a function.

<?php
function welcomeUser($user, $greeting = 'Welcome, ') {
echo "$greeting $user.";
}
welcomeUser('Mike');
?>

The output from the PHP script above is shown below.

Welcome, Mike.

Passing Parameters by Reference in Functions

When you pass a variable as a reference and the function modifies the parameter, the original variable is modified. When a variable is passed to a variable as an parameter to a function, by default, PHP passes ony the value and not the variable itself.

To pass a variable as a reference, prefix a variable name with an ampersand (&) before the dollar sign ($).

The PHP script below passes the variable as a value.

<?php
function increaseX($x){
$x = $x + 1;
echo 'inside function increaseX = ' . $x .'<br />';
}
$x = 10;
increaseX($x);
echo 'outside function increaseX = ' . $x;
?>

The result that the PHP script outputs is below.

inside function increaseX = 11
outside function increaseX = 10

The value of variable $x was changed by the function increaseX. The value of the variable changed only inside of the function. This is called passing a variable by value.

The PHP script below passes the variable as a reference.

<?php
function increaseY(&$y){
$y = $y + 1;
echo 'inside function increaseY = ' . $y .'<br />';
}
$y = 10;
increaseY($y);
echo 'outside function increaseY = ' . $y;
?>

The output from the PHP script above is shown below.

inside function y = 11
outside function y = 11

The value of variable $y changed inside of the increaseY and also outside of it. This is called passing a parameter as a reference.

Returning Values from Functions

You can specify a specific value or values to be returned using the return keyword. When a return statement is executed, the function immediately returns the value(s) and stops executing the rest of the script.

The PHP script below returns the greater of the 2 values ($num1, $num2).

<?php
function numbers($num1,$num2){
if ($num1 > $num2)
return $num1;
else
return $num2;
}
$max = numbers(10,20);
echo $max;
?>

The output from the PHP script above is shown below.

20

The PHP script below is the same as the PHP script above, except that the lesser of the 2 values ($num1, $num2) is returned

<?php
function numbers($num1,$num2){
if ($num1 < $num2)
return $num1;
else
return $num2;
}
$max = numbers(10,20);
echo $max;
?>

The output from the PHP script above is shown below.

10

The PHP script below shows you how to return multiple values from a function.

<?php
function primaryColors(){
$colors = array('red', 'green', 'blue');
return $colors;
}
$colors = primaryColors();
print_r($colors);
?>

The output from the PHP script above is shown below.

Array ( [0] => red [1] => green [2] => blue )

This tutorial was last updated: 11-24-09