Define Functions

You can define a Functions using the following syntax -

            function foo($arg1, $arg2, $arg3, ...., $argN)
            {
                // Body
            }
        

Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.

            $makefoo = true;
            /* We can't call foo() from here  since it doesn't exist yet, but we can call bar() */
            bar();
            if ($makefoo) {
                function foo()
                {
                    echo "I don't exist until program execution reaches me.\n";
                }
            }
            /* Now we can safely call foo() since $makefoo evaluated to true */

            if ($makefoo) foo();

            function bar() 
            {
                echo "I exist immediately upon program start.\n";
            }
        
            function foo() 
            {
                function bar() 
                {
                echo "I don't exist until foo() is called.\n";
                }
            }
            
            /* We can't call bar() yet
                since it doesn't exist. */
            
            foo();
            
            /* Now we can call bar(),
                foo()'s processing has
                made it accessible. */
            
            bar();
        

All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa. PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions.

Calling Functions

As you saw already in the above example, you just give the name and put arguments inside the parenthesis if the function has parameters. like this -

            function one(){
                // Body
            }
            function two($arg1){
                // Body
            }
            function three($arg1, $arg2){
                // Body
            }
            // Now you can call these methods using the following statement --
            one();
            two($arg1);
            three($arg1, $arg2);
        

Note that you are calling these methods from directly outside of the functions. What if you are calling from a function? Well, it's same -

            function one($arg1){
                // Body
            }
            function two(){
                one($arg1);
            }
        

This is good as long as these functions are outside the class. When you have a class and multiple methods defined inside the class and you are calling a method from the another method of the same class, the calling statement is little different. You use $this->methodName([arguments]) syntax to call a method of the same class. You will learn more about class in later section.

Returning Value

A function can return a value. A function can return only one value. If you want a function to return a value just use return statement. You can return any value that is supported by PHP, from small chunk of literal to Big data, there's no limit. If you use return statement, the function ends its execution immediately and pass control back to the line from which it was called.

            function foo(){
                // Statements...
                return $value;
            }

            $returnedValue = foo();
        

Now, $returnedValue contains the value that is returned from the function foo.

By default, a function returns null if we don't use return statement.

If you want to return multiple value, you can return a single array that contains multiple value.