PHP  - Functions in PHP - How to create and how to use
Categories - PHP Tags - PHP   Maniruzzaman Akash   4 months ago   189   0

PHP - Functions in PHP - How to create and how to use

What is Function in Programming?

In programming aspects, A Functions is a chunk of Code which can be executed multiple times without writing it again and again.
Or, A Functions is a chunk of code which can do specific type of task.

Functions in PHP

In PHP there are almost 1000+ built in functions. We’ve already used some of the built in functions in our previous chapter about - Math, String and Array Built in Functions.

Today, we’ll learn Custom user defined function in PHP.

How to Create &  Use Custom User defined Function in PHP

Let’s create a simple custom user defined function in PHP -

<?php
function funtionName( $argument1, $argument2, .... ) {
   // codes
}

// Use this function
funtionName();
  1. To start any function in PHP, we start with function keyword.
  2. After then adding a space and give function name, here - functionName would be your custom function name, eg: printHello(), calculateAge("2000-01-01") etc.
  3. Function name must be start with letter or underscore
  4. Function name is not case-sensitive.
  5. Inside the first parenthesis (), we can pass any number variable, where we’ld call it. We would call that Argument. So, we can pass any number of argument.
  6. After the argument, there would be second curly braces start and stop {}.
  7. Inside the second curly braces {} we can write our specific tasks code.

Let’s Check some Real-life Function Example -

Example 1: Make a function that Print Hello World -

function printHelloWorld() {
   echo "Hello World";
}

// Use this
printHelloWorld(); // Hello World

Example 2: Make a function that can tell if Age is Child, Adult or Old

function getAge($age) {
   if ($age >= 0 && $ageage < 18) {
	   echo "You are aaaa Child";
   } elseif ($age >= 18 && $age < 50) {
	   echo "You are an AdAdultult";
   } elseif ($age >= 50) {
	   echo "You are Old";
   } 
}

// Use this
getAge(25); // You are an Adult

getAge(16); // You are a Child

getAge(70); // You are Old

Argument in Function

In our above Second example, we’ve passed an age integer value while we use this function.

function getAge($age) {
 // codes
}

This passed $age variable is the Argument. We can use as many argument as our necessity in functions.

But, practically it’s not good to use more than 4-5 arguments, cause thus makes the code more unreadable. Maximum 2-3 arguments for a function is standard.

Function Argument Pass by Value

<?php
function getPassByValue($value)
{
    $value = $value * 2;
}

$count = 5;
getPassByValue($count);

echo "Pass by value::" . $count . "<br>"; // 5

Normally, if we pass any variable as argument to a function, then we actually pass the value of the variable. And whatever, we do inside that function, does not effect the passed variable.
This is the Passed by value example. Normally we use this kind of stuffs most of the time.

Function Pass by Reference

<?php
function getPassByReference(&$value)
{
    $value = $value * 2;
}

$count = 5;
getPassByReference($count);

echo "Pass by reference::" . $count; // 10

Now to pass the reference of the variable, we can do that by adding & before function’s argument name. Like the above example -

function getPassByReference(&$value)

And from calling part, we create a variable $count = 5, but after calling that function - getPassByReference($count); After that the $count variable changed to 10, that’s because, we passed the reference of that variable. And after updating that reference inside the function, we actually updated the $count variable.
This is the Pass by reference.

For more -

  1. https://www.php.net/manual/en/language.references.whatare.php
  2. https://www.php.net/manual/en/language.references.arent.php
  3. https://www.php.net/manual/en/language.references.pass.php

Recursive Function

There is a special type of function which is called directly or indirectly by the own function is Recursive function.

Example of a Recursive Function


<?php
function printNumberUpto20($a)
{
    if ($a <= 20) {
        echo "$a\n";
        printNumberUpto20($a + 1); // This call is called Recursive function call
    }
}

// Call that function once
printNumberUpto20(1);

?>

So, in Recursive function there would be some criteria -

  1. Own function will be called. For the above example, look printNumberUpto20($a + 1); the function was called itself.
  2. Function must need to break in a condition. For the above example - $a <= 20 is that condition
Previous
PHP Bonus - Online Judge Problem Solving Using PHP
Next
PHP Loosely Type Language - Strict type from PHP 7