Functions in PHP : Tutorial


A function can be defined as a self-contained block of statements that perform a coherent task.

If a set of PHP statements does a cetain task (e.g finds if the number is even or odd) we can group it together as a function and hire it for doing that task when ever needed.


Syntax :
function functionName($argument1, $argument2,....,$argumentN) {

 //Statements
 
}
function is a pre-defined keyword in PHP.
Function can takein None or N arguments
{ denotes start of a function body whereas } denotes the end of function body.

Function names in PHP are case-insensitive.
Function naming convenstions follow the same rules as that of variables in PHP.


Example 1 : functions with no argruments
In the below example we have defined two functions greetings() and myDetails(). We call the function by the function name followed by () and a ; (semicolon). Note that the function name is case-insensitive.

We may call a function as many times as needed.
<?php

function greetings() {
	
	echo "Hello there!<br>";
	
}

function myDetails() {

	echo "My Name is Mike.<br>";
	echo "In 19 years old.<br>";

}

greetings(); //function call

//Note that function names are case-insensitive
mydetails();

?>
Output

Hello there! My Name is Mike. In 19 years old.


Example 2 : functions with argruments
In the below example function myDetails() takes in two argruments name and age.

Note that we call the function by passing two parameters/agruments myDetails($name,$age);
<?php

function greetings() {
	
	echo "Hello there!<br><br>";
	
}

function myDetails($name,$age) {

	echo "My Name is $name.<br>";
	echo "In $age years old.<br><br>";

}

greetings();

$name = "Natalia";
$age = 21;

//call 1
myDetails($name,$age);

$Myname = "Thomas";
$Myage = 24;

//call 2
myDetails($Myname,$Myage);

?>
Output

Hello there! My Name is Natalia. In 21 years old. My Name is Thomas. In 24 years old.


Example 3 :
<?php

function evenOdd($no) {

	if($no/2 == 1)
	{
		echo "$no is Odd.<<br>";
	}
	else 
	{
		echo "$no is Even.<br>";
	}

}

function addition($a,$b) {
	
	$sum = $a+$b;
	echo "$a + $b = ".$sum."<br>";
	
}

function subtraction($a,$b) {
	
	$diff = $a+$b;
	echo "$a - $b = ".$diff."<br>";
	
}

function multiplication($a,$b) {

	echo "$a * $b = ".$a*$b."<br>";

}

function division($a,$b) {

	echo "$a / $b = ".$a/$b."<br>";

}

function modulus($a,$b) {

	echo "$a % $b = ".$a%$b."<br>";

}

$no1 = 20;
$no2 = 10;

evenOdd($no1);
evenOdd($no2);
addition($no1,$no2);
subtraction($no1,$no2);
multiplication($no1,$no2);
division($no1,$no2);
modulus($no1,$no2);

?>
Output

20 is Even. 10 is Even. 20 + 10 = 30 20 - 10 = 30 20 * 10 = 200 20 / 10 = 2 20 % 10 = 0


Functions returning values

A PHP function can return values by return statement.

A function can return values of any type arrays,integers,strings and objects.

When a return statement is encountered within a function it causes the function to end its execution immediately and pass control back to the line from which it was called.

Syntax :
function functionName($argument1, $argument2,....,$argumentN) {

 //Statements
 
 return value;
 
}

If a function has no return is by default value NULL is returned.

<?php

function evenOdd($no) {

}

	
evenOdd($no1);

?>
Output