Page 91 - Open Soource Technologies 304.indd
P. 91

Unit 4: Functions



                                                                                                  Notes
                          In the condition of default parameters the default values you specify must be
                          a constant value, such as a scalar, array with scalar values, or constant.

            4.4.4 Variable Parameters

            A function may require a variable number of arguments. For example, the get_preferences( )
            example might return the preferences for any number of names, rather than for just one. To
            declare a function with a variable number of arguments, leave out the parameter block entirely.
            function get_preferences( ) { // some code }
            PHP provides three functions you can use in the function to retrieve the parameters passed to it.
            func_get_args( ) returns an array of all parameters provided to the function, func_num_args( )
            returns the number of parameters provided to the function, and func_get_arg( ) returns a specific
            argument from the parameters.
            $array = func_get_args( ); $count = func_num_args( ); $value = func_get_arg(argument_number);
            In Example  the count_list( ) function takes in any number of arguments. It loops over those
            arguments and returns the total of all the values. If no parameters are given, it returns false.
                   Example: Argument counter

            function count_list( ) { if(func_num_args( ) == 0) { return false; } else { for($i = 0; $i < func_num_
            args( ); $i++) { $count += func_get_arg($i); } return $count; } } echo count_list(1, 5, 9);

            The result of any of these functions cannot directly be used as a parameter to another function.
            To use the result of one of these functions as a parameter, you must first set a variable to the
            result of the function, then use that in the function call. The following expression will not work:
            foo(func_num_args( ));
            Instead, use:

            $count = func_num_args( ); foo($count);
                          When you a call a function with default arguments, after you omit a default
                          function argument, you must emit any following arguments. This also means
                          that following a default argument in the function’s definition, all other
                          arguments must also be declared as default arguments.

            4.4.5 Missing Parameters
            PHP lets you be as lazy as you want—when you call a function, you can pass any number of
            arguments to the function. Any parameters the function expects that are not passed to it remain
            unset, and a warning is issued for each of them:
            function takes_two( $a, $b ) { if (isset($a)) { echo “ a is set\n”; } if (isset($b)) { echo “ b is set\n”; } }
            echo “With two arguments:\n”; takes_two(1, 2); echo “With one argument:\n”; takes_two(1);With
            two arguments: a is set b is set With one argument: Warning: Missing argument 2 for takes_two
            ( ) in /path/to/script.php on line 6 a is set.

                          $_GET is a ‘superglobal’, or automatic global, variable. This simply means that
                          it is available in all scopes throughout a script. There is no need to do global
                          $variable; to access it within functions or methods.
            4.5 Return Values


            We know that we must get the value out of the function (instead of just displaying it) in order for
            us to keep using it and keep working with it even if the function has finished execution. In order



                                             LOVELY PROFESSIONAL UNIVERSITY                                    85
   86   87   88   89   90   91   92   93   94   95   96