w3resource

PHP User Defined Function

Description

In all programming and scripting language, a function is a block of statements that can be used repeatedly in a program. In PHP, the concept of the function is the same as in another language like 'C'. There are more than 1,000 in-built functions into the standard PHP distribution. Besides these, we can define functions as per our requirements. These are called 'User Defined Function'.

Syntax:

 
function function-name()
 {
    statement 1 :
    statement 2 :
    statement 3 :
     ......
 }

Elements of a function

function: A function declaration starts with the special word 'function'.

Name of the  function:

The function name is defined by the user.

A valid function name starts with a letter or  underscore, followed by any number of letters, numbers, or underscores.

Remember that function names are case-insensitive.

Opening and Closing curly braces ({  } )

The function body enclosed within a pair of braces which may contain variable names an actual function code. The opening curly brace ( { ) indicates the beginning of the function code and the closing curly ( } ) brace indicates the termination of the function.

Example: PHP function

<?php
function myfunction()
{
echo "Good Morning";
}
myfunction();
?>

When we call the above function it will print Good Morning

View this example in browser

Example: Functions within functions

<?php
function function1()
{
function function2()
{
echo "Good Morning <br>";
}
}
function1();
function2();
?>

In the above example a function, function1() is declared and another function function2() is declared within function1(). Now execute function1() first and then function2() which print "Good Morning", executing function1() makes funtion2() accessible. Therefore we can not call function2() independently without calling function1().

View this example in browser

PHP Function arguments, Returning Values

Function arguments

In PHP information are passed to functions through argument list, which is a comma-delimited list of expression. There are three different ways of passing arguments to a function, arguments by value (the default), passing by reference, and default argument values.

Passing arguments by value

The argument can be any valid expression. The expression is evaluated and its value assigned to the appropriate variable in the function. In the following function $a assigned the value 10 and $b assigned the value 20:

function add($a, $b)
{
...
}
add(10, 20);

Passing arguments by reference

By default, the function arguments are passed by value. If you want to allow a function to change its arguments, you must pass the arguments by reference. To pass an argument to a function as a reference, simply add an ampersand (&) character before the variable name.

<?php
function cube(&$x)
{
$x = $x * $x * $x;
}
$result = 5;
cube($result);
echo $result;
?>

Output:

125

View this example in browser

Default arguments values

In the following example the function wage() use a default parameter. When we have called the function without arguments it takes the default value as an argument.

<?php
function wage($minwage= 100)
{
echo "The wage is : $minwage <br />" ;
}
wage(200);
wage();
wage(100);
?>

Output:

The wage is : 200
  The wage is : 100
  The wage is : 100 

View this example in browser

PHP: Returning Values

In PHP values are returned by a return statement. The return values must be specified in the variable. If the statement is called within a function the function is terminated immediately and pass control back to the previous position from which it was called.

The return statement can return any type of data.

Example:

In the following example, a function accepts a number and calculate the cube of that number using the return statement.

<?php
function cube($x)
{
return $x * $x * $x;
}
echo "The cube of 4 is : ".cube(4)."<br />";
echo "The cube of 9 is : ".cube(9)."<br />";
echo "The cube of 20 is : ".cube(20)."<br />";
?>

Output:

The cube of 4 is : 64
  The cube of 9 is : 729
  The cube of 20 is : 8000

View this example in browser

Previous: require_once, include_once
Next: PHP Object Oriented Programming



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php/function/user-defined-function.php