PHP function - What is function in php?

A function is a piece of code which takes one or more input in the form of parameter and does some processing and returns a value.

A function is a block of statements that can be used repeatedly in a program.
A function will not execute immediately when a page loads.
A function will be executed by a call to the function.

  • Creating Function
  • Calling Function
  • Creating Function :- We create a function its name should starts with keyword function and all the php code should be put inside { } braces.

    File name : index.php

    function show() {
    echo "ittutorial is the really nice it tutorial website.";
    }

    show(); // call the function
    ?>

    Example

    File name : index.php

    Syntax :-
    Function functionName(list of parameters)
    {
    // function body.
    //statements.
    return return_value;
    }
    Example :-
    function write_name( )
    {
    echo "mahtab";
    }

    echo "My name is ";
    // calling function
    write_name( );
    ?>
    Output : my name is mahtab

    Function with parameters :-

    A parameter is a just like a variable. Parameter are specified after the function name, inside the parentheses.

    File name : index.php

    function write_name($fname)
    {
    echo $fname .?alam
    ?;
    }
    echo "my name is ";
    write_name("mahtab");
    echo "my father name is ";
    write_name("habib");
    ?>

    Output :-

    my name is mahtab
    My father name is habib.

    Parameterised Function.

    File name : index.php

    You can pass as many as parameters. These parameters work like variables inside your function.
    Eg. function add($num1, $num2)
    {
    $sum = $num1 + $num2;
    echo "result = $sum";
    }
    add(20,30);
    ?>

    Output :-

    result = 50

    PHP Default Argument Value

    File name : index.php

    function setHeight($minheight = 5) {
    echo "your height is : $minheight
    ";
    }

    setHeight(6);
    setHeight();
    setHeight(7);
    setHeight(4);
    ?>

    Output :-

    your height is : 6
    your height is : 5
    your height is : 7
    your height is : 4

    Using default arguments

    File name : index.php

    You can pass default arguments in the function using an equal sign. For ex.
    Function display($greeting, $message = "Hi")
    {
    echo $greeting;
    echo $message;
    }
    Now if you don't pass anything for the second argument to the display function, the default argument is automatically used.

    Example :-

    function display($param = null)
    {
    echo $param;
    }
    display("hi");
    display( );
    ?>

    Functions - Returning values

    File name : index.php

    function sum($x, $y) {
    $z = $x + $y;
    return $z;
    }
    echo "5 + 10 = " . sum(5, 10) . "
    ";
    echo "7 + 13 = " . sum(7, 13) . "
    ";
    echo "2 + 4 = " . sum(2, 4);
    ?>

    Output :-

    5 + 10 = 15
    7 + 13 = 20
    2 + 4 = 6

    File name : index.php

    function add($x, $y)
    {
    $total = $x + $y;
    //echo "result = $sum";
    return $total;
    }
    echo "20 + 30 = " .add(20,30);
    ?>


    Output :-

    20 + 30 = 50

    Passing arguments by reference :-

    A references to the variable is manipulated by the function. Rather than a copy of the variables value. Any changes made to an argument in these cases will change the value of the original variable. You can pass an argument by adding an ampersand (&) to the variable name in either the function call or the function definitions.

    File name : index.php

    function add($num)
    {
    $num = $num + 5;
    }
    function addsix(&$num)
    {
    $num +=6;
    }
    $orignum = 10;
    add($orignum);
    echo "original value is $orignum
    ";
    addsix($orignum);
    echo "original value is $orignum
    ";
    ?>

    Output :-

    original value is 15
    original value is 21

    Dynamic Function call :-

    It is possible to assign function names as strings to variables and then treat these variables exactly as you would the function name itself.

    File name : index.php

    function say( )
    {
    echo "hello mahtab
    "
    }
    $holder = "say";
    $holder( );
    ?>

    Output :-

    hello mahtab

    Passing array to function

    You can pass array to function just like string and numbers.

    File name : index.php

    $scores = array(65,32,78,98,66);
    averager($scores); // calling function
    function averager($arr) {
    $total = 0;
    foreach ($arr as $value) {
    $total = $total+$value;
    }
    if(count($arr)>0) {
    echo "the average was ", $total / count($arr);
    }
    else {
    echo "no elements";
    } }
    ?>





    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here