Functions play an important role for writing different PHP scripts and I believe, it is very good and specialized approach to design any PHP applications in a way that can easily be configured on different environments.Thanks to PHP which provide functions which made developers life easy.
Let’s get dive in some useful function handling functions:
1. create_function:
It is a basic function of function handling functions.This function will create an anonymous PHP function.It returns a unique function name as a string, or FALSE if create an error.
SYNTAX:
1 2 3 | string create_function ( string $args , string $code ) |
EXAMPLE:
1 2 3 4 | $triangle = create_function($b,$h', 'return "triangle = " . ($b*$h)/2;'); echo $triangle(4,6); |
OUTPUT: 12
2. function_exists:
This function is useful to check whether a function is defined or not.It returns true if the function has been defined
SYNTAX:
1 2 3 | bool function_exists ( string $function_name ) |
EXAMPLE:
1 2 3 4 5 6 7 8 9 10 11 12 | function func_test(){ echo "Welcome"; } if (function_exists('func_test')) { echo "Function is exists"; } else { echo "Function isn't exists"; } |
OUTPUT: Function is exists
3. call_user_func_array:
It is one of my favorite PHP because it is very useful for us.this function call a function with an array of parameters. We can say it’s an alternate method for calling functions and a way to dynamically call functions and methods at run-time. Its returns a valid function name and false if error.
SYNTAX:
1 2 3 | mixed call_user_func_array ( callback $callback , array $param_arr ) |
Generally we are calling a function like
function func_name(parameters)
so, with this We can call the function like
1 2 3 | call_user_func_array ('function_name', array (parameter1,parameter2, ...)); |
EXAMPLE:
1 2 3 4 5 6 7 8 | function Box($width,$height,$depth) { $b = $width*$height*$depth; echo $b; } call_user_func_array('Box', array('width' => $w, 'height' => $h, 'depth' => $d)); |
4. call_user_func:
This is the interesting function in function handling functions and its call a user defined function given by the first parameter. It is useful to call a function whose name you will not know until runtime.
SYNTAX:
1 2 3 | mixed call_user_func ( callback $function [, mixed $parameter [, mixed $... ]] ) |
EXAMPLE:
1 2 3 4 5 6 7 | function Box($width) { echo “Box Width:”.$width; } call_user_func('Box', array('width' => $w)); |
5. func_num_args:
With this function, you can easily get information about the number of arguments passed to the function.It returns the number of arguments passed into the current user-defined function.
SYNTAX:
1 2 3 | int func_num_args ( void ) |
EXAMPLE:
1 2 3 4 5 6 7 8 9 | function combined() { $num_arg = func_num_args(); echo "Number of arguments: $num_arg<br />"; } combined('A,'B','C'); |
OUTPUT:
Number of arguments: 3
6. func_get_arg & func_get_args:
func_get_arg is a PHP function handling function will provide you information to get an array of all arguments passed to a user-defined function. func_get_args is to get the specified argument from a user-defined function’s argument list.
Function arguments are counting starts from zero
SYNTAX:
1 2 3 4 | mixed func_get_arg ( int $arg_num ) array func_get_args ( void ) |
EXAMPLE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function combined() { $num_arg = func_num_args(); if ($num_arg > 0) { echo "First argument is: " . func_get_arg(0) . "<br />n"; $arg_list = func_get_args(); for ($i = 0; $i < $num_arg; $i++) { echo "Argument $i is: " . $arg_list[$i] . "<br />"; } } } combined('A,'B','C'); |
OUTPUT:
First argument is: A
Argument 0 is: A
Argument 1 is: B
Argument 2 is: C
NOTE: This function returns passed arguments only, and does not used for non-passed arguments.
7. get_defined_functions:
This function in useful to get an array of all defined functions.It returns a multidimensional array having a list of all defined functions.
SYNTAX:
1 2 3 | array get_defined_functions ( void ) |
EXAMPLE:
1 2 3 4 5 6 7 8 9 | function sample_test_func() { return; } $array = get_defined_functions(); print_r($array); |
OUTPUT:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | Array ( [internal] => Array ( [0] => zend_version [1] => func_num_args [2] => func_get_arg [3] => func_get_args [4] => strlen [5] => strcmp [6] => strncmp ... ... ) [user] => Array ( [0] => sample_test_func ) ) |
If you still have any questions or other tips to share, Do Share your opinion here in the comments!
Comments (1)