User Functions

Contact Us or call 1-877-932-8228
User Functions

User Functions

User functions are used to make common tasks easier and to make code more modular and easier to read. Unlike other identifiers in PHP, functions are case insensitive.

Defining and Calling Functions

A simple function is defined as follows.

Syntax

function myfunct()
{
	do this;
	do that;
	do this other thing;
}

Like built-in functions, user functions can receive parameters. To define a function with parameters, place receiving variables in the parentheses.

Syntax

function addNums($param1, $param2, $param3)
{
	$sum = $param1 + $param2 + $param3;
	echo 'The sum is ' . $sum;
}

User functions can also return values.

Syntax

function addNums($param1, $param2, $param3)
{
	$sum = $param1 + $param2 + $param3;
	return $sum;
}

User functions are called in the same way that built-in functions are. For example, the following code calls the addNums() function to get the sum of three numbers.

Syntax

$total = addNums(1,3,5);

See ReusingCode/Demos/SimpleUDF.php to see the above code in action.

Default Values

You can make function parameters optional by assigning default values to them as shown in the example below.

Code Sample:

ReusingCode/Demos/DefaultValues.php
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Simple User-defined Function</title>
</head>

<body>
<?php
function addNums($param1=0, $param2=0, $param3=0)
{
	$sum = $param1 + $param2 + $param3;
	return $sum;
}

$Total = addNums(1,3);

echo $Total;
?>
</body>
</html>

In this case, if you don't pass a value into the function for one or more of the parameters, the default value of 0 will be used. When defining a function, all required parameters should precede optional parameters.

Variable Scope

In PHP, variables declared outside of functions are not available by default inside of functions. The following code illustrates this:

Code Sample:

ReusingCode/Demos/LocalVars.php
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Local Variables</title>
</head>

<body>
<?php
$a = 10;
$b = 5;

function incrNumBy()
{
	$a += $b;
}
incrNumBy(); //results in two warnings as $a and $b are
					//undefined in the function scope
echo $a; //outputs 10 to the browser
?>
</body>
</html>

To make the variables available to the function, they must be declared within the function as global variables using the global keyword.

Code Sample:

ReusingCode/Demos/GlobalVars.php
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Global Variables</title>
</head>

<body>
<?php
$a = 10;
$b = 5;

function incrNumBy()
{
	global $a,$b;
	$a += $b;
}
incrNumBy(); 
echo $a; //outputs 15 to the browser
?>
</body>
</html>

By Reference vs. By Value

By default, variables are passed to functions by value, meaning that the function's receiving variables get copies of the values received rather than pointers to them. If the receiving variables are modified, the passed variables remain unaffected. The following code illustrates this.

Code Sample:

ReusingCode/Demos/ByValue.php
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>By Value</title>
</head>
<body>
<?php
	$a = 10;
	$b = 5;
	function incrNumBy($num,$incr)
	{
		$num += $incr;
	}
	
	incrNumBy($a,$b);
	echo $a; //outputs 10 to the browser
?>
</body>
</html>

The above code outputs "10" to the browser. Although $num was incremented by 5, $a was unaffected by the function call. To pass a variable by reference, put an ampersand (&) before the parameter in the function definition.

Code Sample:

ReusingCode/Demos/ByReference.php
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>By Reference</title>
</head>
<body>
<?php
	$a = 10;
	$b = 5;
	function incrNumBy(&$num,$incr)
	{
		$num += $incr;
	}
	
	incrNumBy($a,$b);
	echo $a; //outputs 15 to the browser
?>
</body>
</html>

This time the function outputs "15" because $num references the variable $a itself. So, any change in $num will affect $a.

Next