Functions

goto
November 14, 2018
Classes and Objects
November 14, 2018
  • User-defined functions
  • Function arguments
  • Returning values
  • Variable functions
  • Internal (built-in) functions
  • Anonymous functions

    User-defined functions

    A function may be defined using syntax such as the following:

    Example #1 Pseudo code to demonstrate function uses

    <?php
    function foo($arg_1, $arg_2, /* ..., */ $arg_n)
    {
    echo "Example function.\n";
    return $retval;
    }
    ?>

    Any valid PHP code may appear inside a function, even other functions and class definitions.

    Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.

    Function arguments

    Information may be passed to functions via the argument list, which is a comma-delimited list of expressions. The arguments are evaluated from left to right.

    PHP supports passing arguments by value (the default), passing by reference, and

    Example #1 Passing arrays to functions

    <?php
    function takes_array($input)
    {
    echo "$input[0] + $input[1] = ", $input[0]+$input[1];
    }
    ?>

    Passing arguments by reference

    By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). To allow a function to modify its arguments, they must be passed by reference.

    To have an argument to a function always passed by reference, prepend an ampersand (&) to the argument name in the function definition:

    Example #2 Passing function parameters by reference

    <?php
    function add_some_extra(&$string)
    {
    $string .= 'and something extra.';
    }
    $str = 'This is a string, ';
    add_some_extra($str);
    echo $str;    // outputs 'This is a string, and something extra.'
    ?>

    Default argument values

    A function may define C++-style default values for scalar arguments as follows:

    Example #3 Use of default parameters in functions

    <?php
    function makecoffee($type = "cappuccino")
    {
    return "Making a cup of $type.\n";
    }
    echo makecoffee();
    echo makecoffee(null);
    echo makecoffee("espresso");
    ?>

    Returning value

    Values are returned by using the optional return statement. Any type may be returned, including arrays and objects. This causes the function to end its execution immediately and pass control back to the line from which it was called. See return for more information.

    Note:

    If the return is omitted the value NULL will be returned.

    Use of return

    Example #1 Use of

    <?php
    function square($num)
    {
    return $num * $num;
    }
    echo square(4);   // outputs '16'.
    ?>

    A function can not return multiple values, but similar results can be obtained by returning an array.

    Example #2 Returning an array to get multiple values

    <?php
    function small_numbers()
    {
    return array (0, 1, 2);
    }
    list ($zero, $one, $two) = small_numbers();
    ?>

    To return a reference from a function, use the reference operator & in both the function declaration and when assigning the returned value to a variable:

    Example #3 Returning a reference from a function

    <?php
    function &returns_reference()
    {
    return $someref;
    }
    $newref =& returns_reference();
    ?>

    Example #4 Basic return type declaration

    <?php
    function sum($a, $b): float {
    return $a + $b;
    }
    // Note that a float will be returned.
    var_dump(sum(1, 2));
    ?>

    Variable functions

    PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth.

    Variable functions won’t work with language constructs such as echoprintunset()isset()empty()include,require and the like. Utilize wrapper functions to make use of any of these constructs as variable functions.

    Example #1 Variable function example

    <?php
    function foo() {
    echo "In foo()<br />\n";
    }

    function bar($arg = '')
    {
    echo "In bar(); argument was '$arg'.<br />\n";
    }

    // This is a wrapper function around echo
    function echoit($string)
    {
    echo $string;
    }

    $func = 'foo';
    $func();        // This calls foo()

    $func = 'bar';
    $func('test');  // This calls bar()

    $func = 'echoit';
    $func('test');  // This calls echoit()
    ?>

Sondiva Technologies
Sondiva Technologies
Sondiva Technologies started in 2011 with a vision of providing Quality based Web Hosting and Web Designing Services in Ghaziabad, Delhi NCR. Backed up by full stack developers and designers , with over 500+ Web Projects. They work on opensource Platforms like Wordpress, Magneto, Prestashop, Opencart and Core PHP. Other services are : Responsive Web Designing Domain Registration Services Dedicated & SSD Hosting Virtual Private Servers Google GSuite Mail Servers E-Commerce Shopping Portal Development Web Designing & Application Courses SEO & Digital Marketing

Leave a Reply

Your email address will not be published. Required fields are marked *

Shop Now