0

Functions

September 19, 2022
Share

A function is just a small program written and called upon to use whenever its work is needed. I know, you may be confused now but do not worry 🙈. If you have ever used MS. Excel, chances are that you have worked with simple functions like SUM, AVERAGE, MIN, MAX, etc. When using them, you just use an equal sign and then the function name. Afterwords, the computation is done for you with very little effort. These functions were written with the logic of addition, subtraction, etc and we just invoke this logic whenever we use them. Okay, enough of Excel.

In PHP, we have thousands of in-built functions we can use to perform a lot of tasks. They include echo(), abs() for positive values, date() for dates, max() for highest value, min() for returning lowest value, jdmonthname() to return name of the month, MySQLi for accessing MySQL database servers, etc. For a complete list of PHP’s inbuilt function, visit this page.

Lets have an example of such functions in the snippet below.

<?php

 	$day = date('l'); 
 	echo "Today is $day";


?>

The code in the snippet above returns the day of the week and we use the date() function to return it.

User Defined Functions

Apart from in-built functions, we also have user defined functions. You can define your own functions and you use them in your own program or even other people can use, why not, save the planet 😍. To write your functions, lets analyze the code snippet below:

<?php


 	function sayHi($firstname) {
 		echo "Hello, $firstname!";
 	}

 	sayHi("Peter Smith");

?>

We start with the keyword function followed by the function name, in our case it is sayHi (you can call it anything you want). This is followed by a bracket and inside we write the variable we need the user to provide values for (this is what we call arguments or parameters), for our case it is $firstname. You can have as many parameters as you wish. This is followed by curly brackets and inside these is where we write what we need the function to output whenever invoked, for our case, we want it to output Hello and the name the user provides. That is the end of the function and that is how they are written in PHP. To use this function sayHi, we call it or invoke it, and this is what you see in the next line i.e., sayHi(“Peter Smith”). Peter Smith is the name the user has supplied to this function (you can write your own name). On running, you can see that the program outputs Hello Peter Smith!.

Example 2: Let us have a function that adds two numbers he user has provided and later output the answer to the screen.

<?php

 	function add($number1, $number2) {
                $result = $number1 + $number2;
 		echo "The answer is $result";
 	}

 	add(239, 21.78);

?>

Default Argument Values

Sometimes we may supply default values to function arguments and in case the user doesn’t provide a value for that argument, the default value will be considered. For example, lets modify the previous function and we assign 5 as the default value for $number2.

<?php

 	function add($number1, $number2 = 5) {
                $result = $number1 + $number2;
 		echo "The answer is $result";
 	}

 	add(20);

?>

In the code above, we only supplied value for $number1 as 20 but the answer is 25. Simply because, PHP just used the default value for $number2 which is 5.

Now that you have learnt how to write functions, congratulations 🪘. You are on your way to becoming a real coder 👨‍💻. Continue learning!

Back To Top