PHP Tutorial Functions with Stone River eLearning

A function is a block of code that can be used or called from anywhere in the PHP script once it has been declared in it. The major use of function is to make the script smaller, easier to understand and more compact. Imagine that we are given the task of adding two numbers using PHP.

Now as programmers, our objective is to write a code that can be used for any two numbers on the number line. Without using functions, we will have to write down the same code again and again for the different set of numbers; an impossible task to achieve. However, what if we could write the code just once and then use this single code again and again for different numbers? Clearly the second option sounds more optimal and can be achieved using functions in PHP.

There are two important terms associated with functions in any programming language, the first one being ‘declaring a function’ and the second one is ‘calling a function’.  Basically, declaring a function is nothing but creating the content inside the function, while calling a function is giving the green light for the function to run. Now to declare a function in PHP, the keyword ‘function’ is required before the name of the function which is to be created. The content or the operation that the function will perform is included inside the braces, { and }. After creating the function, we must give the green light for the function to be used in the code i.e call the function. To call a function we must simply write the name of the function followed by parenthesis and a semicolon. 

Note: A function can be called inside its declaration part, however; this practice must be avoided by beginners as it involves recursion. Further, even if the function is called in itself, it must be called outside once to instruct the compiler to look for the declaration part. 

Eg:

<?php
	function name() // This part is the declaration of the function
	{
		echo ‘Alex’;
	}
	name(); // This is the function call.
?>

Functions in PHP can be executed with parameters or arguments passed to it. These parameters act like variables inside the declaration part of the function. We can pass as many arguments as we want to the function, however the function must know about the number of arguments passed. If for a function, we pass more arguments and specify less variables, or vice versa, then the PHP script will show an error message complaining about the missing value or variable accordingly. 

Eg:

<?php
	function add($num1, $num2) // Takes two arguments from the function call
	{
		$sum = $num1 + $num2; // Adds the two arguments
		return $sum;  // Returns the value received after adding the numbers
	}
	echo add( 10, 30);
?>

It is important to note that in the above example, $num1 and $num2 are used as variables for the function add and the values 10 and 30 are fed to these two variables during the function call. The variable $num1 thus contains the value 10 and the variable $num2 contains the value 30. 

Note: 

The return keyword is used to return the value inside the variable $sum from the function add. This value is then printed on the page as echo keyword is used before the function call. 

The loosely type nature of PHP variable automatically adjusts the data type of the variable to the data type of the value passed to it. Therefore, you can even pass a string or a Boolean value to the function as an argument in the above example. 

In case, we pass more than two values to the function add, then during the declaration part, we must subsequently assign more variables to store these values.

Using functions is a common and highly recommended practice in PHP scripts. Apart from making the code smaller and giving an easy flow to it, functions also allows the programmer to write the code for complex problems with lesser difficulty. Functions are of high utility in writing PHP codes for problems that are aimed at real life scenarios and hence, it is an attractive option for a PHP script. 

GET YOUR FREE PHP EBOOK!

(Visited 105 times, 1 visits today)