Function in PHP

PHP Functions
  • A function is a block of code use for perform certain task or operation.
  • A function is executed only when we call the function, not a program or a code is executed.
  • In PHP 1000+ built-in functions are available. And in PHP you can create your own custom functions.
  • In PHP two types of functions are available.
    1. PHP Built-in functions
    2. PHP User defined functions
    1.PHP Built-in functions
  • PHP has 1000+ built-in functions that you can use directly in your script to be called directly and execute your code.
  • For example, strlen(), print_r(), fopen(), mail() etc.
  • <!DOCTYPE html>
    <html>
    <body>
    <?php
    $bike = array("BMW", "Yamaha", "suzuki");
    print_r($bike);
    ?>
    </body>
    </html>
    

    <?php
    $bike = array("BMW", "Yamaha", "suzuki");
    print_r($bike);
    ?>

    Output:

    Array ( [0] => BMW [1] => Yamaha [2] => suzuki )
    1.PHP User defined functions
  • PHP gives a facility to create a function by the user.
  • PHP user-defined function can be used repeatedly in code or script.
  • A user-defined function is executed only when we call the function, not a program or a code is executed.
  • Syntax
  • function functionName() {
    code to be executed;
    }

    Note: A function name must start with a letter or an underscore. Not a use number or special characters.

  • Example
  • <?php
    function hello_message() {
    echo "Hello PHP coders";
    }
    hello_message();
    ?>

    Output:

    Hello PHP coders

    PHP function arguments

  • Information can be passed to functions through php function arguments.
  • Function arguments are written after the function name, inside the parentheses. You can add one or more arguments as you want, arguments which are separated by a comma.
  • PHP supports Call by Value, Call by Reference, Default argument values and Variable-length argument list.
  • Example
  • <?php
    function BikeName($name) {
    echo "$name </br>";
    }
    BikeName("BMW");
    BikeName("Yamaha");
    BikeName("suzuki");
    ?>

    Output:

    BMW
    Yamaha
    suzuki
  • user-defined function with two arguments
  • <?php
    function BikeName($name,$model,$price) {
    echo "$name => $model => $price </br>";
    }
    BikeName("BMW","G 310 R",299000);
    BikeName("Yamaha","FZ25",135000);
    BikeName("suzuki","Hayabusa",1374000);
    ?>

    Output:

    BMW => G 310 R => 299000
    Yamaha => FZ25 => 135000
    suzuki => Hayabusa => 1374000

    PHP Function: Default Argument Value

  • You can define a default argument value in function
  • Example
  • <?php
    function BikeName($name="BMW") {
    echo "$name </br>";
    }
    BikeName();
    BikeName("Yamaha");;
    BikeName("suzuki");
    ?>

    Output:

    BMW
    Yamaha
    suzuki


    PHP call by reference

  • In PHP passing value as a call by reference.
  • To pass value as a call by reference, you need to use ampersand & symbol before the argument name.
  • Example
  • <?php
    function addString(&$hello)
    {
    $hello .= 'PHP coders';
    }
    $welcome = 'Hello ';
    addString($welcome);
    echo $welcome;
    ?>

    Output:

    Hello PHP coders

    PHP Function: Returning Value

  • In PHP passing value as a call by reference.
  • Example
  • <?php
    function square($n){
    return $n*$n;
    }
    echo "Square of 5 is : ".square(5);
    ?>

    Output:

    Square of 5 is : 25

    Exercise: Other PHP function list click here for see list.