PHP – Functions

 

The real power of PHP comes from its functions; it has more than 1000 built-in functions.

 

PHP User Defined Functions

 

Besides the built-in PHP functions, we can create our own functions.

A function is a block of statements that can be used repeatedly in a program.

A function will not execute immediately when a page loads.

A function will be executed by a call to the function.

 

Create a User Defined Function in PHP

 

A user-defined function declaration starts with the word function:

 

Syntax

 

function functionName() {
code to be executed;
}

 

Note: A function name can start with a letter or underscore (not a number).

Tip: Give the function a name that reflects what the function does!

Function names are NOT case-sensitive.

 

In the example below, we create a function named “writeMsg()”. The opening curly brace ( { ) indicates the beginning of the function code and the closing curly brace ( } ) indicates the end of the function. The function outputs “Robert Method Karamagi”. To call the function, just write its name:

 

Example

 

<!DOCTYPE html>
<html>
<body>

<?php
function writeMsg() {
echo “Robert Method Karamagi”;
}

writeMsg();
?>

</body>
</html>

 

Output

 

 

PHP Function Arguments

 

Information can be passed to functions through arguments. An argument is just like a variable.

Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

The following example has a function with one argument ($fame). When the science() function is called, we also pass along a name (e.g. Physical), and the name is used inside the function, which outputs several different types of sciences.

 

Example

 

<!DOCTYPE html>
<html>
<body>

<?php
function science($name) {
echo “$name Science.<br>”;
}

science(“Physical”);
science(“Social”);
science(“Meteorological”);
?>

</body>
</html>

 

Output

 

The following example has a function with two arguments ($name and $capital):

 

Example

 

<!DOCTYPE html>
<html>
<body>

<?php
function country($name, $capital) {
echo “The capital city of $name is $capital. <br>”;
}

country(“Russia”,”Moscow”);
country(“Canada”,”Ottawa”);
country(“United States of America”,”Washington, D.C”);
?>

</body>
</html>

 

Output

 

 

PHP Default Argument Value

 

The following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument:

Example

<!DOCTYPE html>
<html>
<body>

<?php
function setHeight($minheight = 50) {
echo “The height is : $minheight <br>”;
}

setHeight(350);
setHeight();
setHeight(135);
setHeight(80);
?>

</body>
</html>

 

Output

 

 

PHP Functions – Returning values

 

To let a function return a value, use the return statement:

 

Example

 

<!DOCTYPE html>
<html>
<body>

<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}

echo “5 + 10 = ” . sum(5,10) . “<br>”;
echo “7 + 13 = ” . sum(7,13) . “<br>”;
echo “2 + 4 = ” . sum(2,4);
?>

</body>
</html>

Output