PHP Functions

In this lesson, you will learn about Functions in PHP, and their usages, along with examples to better understand the topic.


PHP Functions

PHP functions are comparable to those of other languages of programming. A function is a piece of code in the form of a parameter that takes one or more inputs, performs some processing, and returns a value. Although PHP has built-in functions, you can also create your own functions.


PHP Built-in Function

A function is a block of code contained within itself that performs a particular job. You can call one of PHP’s many internal or built-in functions to carry out a specific task within your PHP scripts, such as those listed below:

  • gettype()
  • print r()
  • var dump()

PHP User-defined Function

You can define both your custom functions and built-in functions using PHP. It is a technique for producing reusable code packages that carry out specific tasks, which you keep and maintain separately from the main program.


Why are user-defined functions used?

Functions reduce code repetition within a program—a function enables you to extract frequently used code blocks into a single element. By calling a function wherever you want in your script, you can carry out the same task without repeatedly copying and pasting the same block of code.

Functions make the code much simpler to maintain. Since you can use it multiple times, any modifications produced within a function will automatically be enforced at all locations without touching numerous files.

When you break down a program into functions, finding and fixing errors becomes easier because you can understand what caused them and where to look for them. Thus, it becomes much easier to fix errors. Reusing functions in other apps is possible because they are isolated from the script’s main body. To do this, include the PHP file and use the same function in other apps.

In the subsequent section, you’ll see a simple example of how to define your function in PHP.


Create a Function

Syntax of function

function NameofFunction(){ // Name of function
// Code will be processed in parenthesis
}

The user-defined function declaration begins with the word function, is followed by the name of the new function, which is enclosed in parentheses (), and finally, inserts the code for your function between curly brackets.

Function Flowchart

 

Here is a straightforward illustration of a user-defined feature that displays the current date:

Example

<?php
// Defining the todaydayFunc function
function todaydayFunc()
{
    echo " Today's Day = " . date('l', mktime());
}
// Calling the todaydayFunc function
todaydayFunc();
?>

Output

Today's Day = Wednesday

Function Naming Rules

A function name must begin with a non-number letter or underscore character, optionally followed by more letters, numbers, or characters underlined. The names of functions are case-insensitive and cannot be PHP-reserved keywords.


PHP Parameterized Function

PHP Parameterized functions are parameter functions. You can supply the function with any number of parameters. Within your function, the passed parameters act as variables. Inside the parentheses, they are defined after the name of the function. The output relies on the dynamic values carried into the function as the parameters.

You can specify parameters to recognize input values at runtime when you define your function. A function’s parameters serve as placeholders; at runtime, the values (referred to as arguments) passed to the function at the time of invocation take their place. Information can be carried through the argument list to tasks, a comma-delimited expression list. From left to right, the arguments are assessed.

Example

<?php
echo "PHP Parameterized Function Example";
echo "<br>";
echo " ------------Adding (+)------------------ ";
echo "<br>";
//Adding 2 Parameterized number by Function
function add($var1, $var2)
{
    $addFunc = $var1 + $var2;
    echo " The addition of two numbers is = $addFunc <br>";
}
add(700, 200);
echo "<br>";
echo "
------------Subtraction(-)------------------ ";
echo "<br>";
//Subtracting 2 Parameterized number by Function
function subtract($var1, $var2)
{
    $subtractFunc = $var1 - $var2;
    echo "Subtraction of two numbers is = $subtractFunc <br>";
}
add(700, 300);
?>

Output

PHP Parameterized Function Example
------------Adding (+)------------------
The addition of two numbers is = 900

------------Subtraction(-)------------------
The addition of two numbers is = 1000

This concludes the PHP Functions lesson. The next lesson will teach you about Call By Value and By Reference in PHP.