PHP Tutorial


PHP 7 Functions

Author:- w3alert.com
Updated Date:-01-02-2020

PHP Functions

A function is a subprogram in which we write some tasks that perform on the same task.

The PHP functions are comparable to other programming languages. Functions are segments or set of code that is reusable that perform a specific operation. When a function is called, the functions return values ​​whether or not some operation is written to the function. PHP provides over 1000 in-built functions that are used to perform many types of operations.

Importance of functions

Easy to Debug: Functions are a segment of code. If an error is present in a function then it can be checked and fixed easily.

Reusability: PHP functions are written once which can be implemented by several methods as required.

No Code Repetition: The function is written to only one in PHP files and can be used repeatedly so that the code does not have long.

There are two type of functions

  • Built-In Functions
  • User Defined Functions

Built-In Functions

PHP provides several built in functions that are used to manipulate arrays, strings, letters, numbers and so on. 
rand(), strtoupper(), strtolower(), ucfirst(), strlen(), ucwords(), dl(), mysqli_connect(), phpinfo(), gettype(), print_r(), var_dump, etc.

Example:

The following examples of PHP Built-in Functions.

    <!DOCTYPE html>
    <html>
    <body>

    <?php
         
        // It is generate Random Number between two number.
        echo rand(0,5)."<br>"; 

        // Convert string to Upper_case.
        echo strtoupper("john") ."<br>" ;

        // Convert string to Lower_case.
        echo strtolower("JOHN") ."<br>" ;

        // Count string length.
        echo strlen("JohnSmith") ."<br>" ;

    ?>

    </body>
    </html>

User Defined Functions

PHP allows defining its own functions and using these functions can increase the reusability of the code. Programmers write the code as per their requirement and execute it in the main program.

This process is completed in the following sections:

Function declaration
Function calling

Syntax:

    <?php

    function function_name(arg) {

       # Group of code Statement.
    }

    ?>

Example:

The following example of PHP User defined Functions.

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    // Function Declaration.

    function firstFunction() {     

        echo "your first function is working"; 
        
    }
        firstFunction(); // Function Calling.
        
    ?>

    </body>
    </html>


PHP Function Arguments

The argument is like a variable, it contains a string, array, number, etc. Information in functions is sent by an argument. In this, developers can pass multiple arguments to the built function by appending a comma according to their requirements.

Example:

The following example of the PHP Function Argument.

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    function getName($fname, $lname) {

        echo "your name is $fname $lname";
        
    }
        getName ("John","Smith");

    ?>

    </body>
    </html>

PHP Default Argument Value

If you do not pass any value and argument to the function then it uses the default argument value.

Example:

The following example of PHP Default Argument Value.

    <!DOCTYPE html>
    <html>
    <body>

    <?php
    function setMin(int $minvalue = 25) {
        echo "The value is : $minvalue <br>";
    }

    setMin(150);
    setMin(100);
    setMin();
    setMin(50);
    ?>

    </body>
    </html>