Back to Top

Function handling Functions in PHP

Function handling Functions in PHP

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:

string create_function ( string $args , string $code )

EXAMPLE:

$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:

bool function_exists ( string $function_name )

EXAMPLE:

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:

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

call_user_func_array ('function_name', array (parameter1,parameter2, ...));

EXAMPLE:

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:

mixed call_user_func ( callback $function [, mixed $parameter [, mixed $... ]] )

EXAMPLE:

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:

int func_num_args ( void )

EXAMPLE:

function combined()
{
   $num_arg = func_num_args();
   echo "Number of arguments: $num_arg
"; } 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:

mixed func_get_arg ( int $arg_num )
array func_get_args ( void )

EXAMPLE:

function combined()
{
   $num_arg = func_num_args();
   if ($num_arg > 0) {
       echo "First argument is: " . func_get_arg(0) . "
n"; $arg_list = func_get_args(); for ($i = 0; $i < $num_arg; $i++) { echo "Argument $i is: " . $arg_list[$i] . "
"; } } } 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:

array get_defined_functions ( void )

EXAMPLE:

function sample_test_func()
{
    return;
}
$array = get_defined_functions();

print_r($array);

OUTPUT:

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)

  1. This is rlealy cool. How does this work? Is the Related Posts code already in the current version of WordPress, allowing this code to activate it? Or does this work another way?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Hello world

Updated 1 month ago

Nick Carter

In depth Understanding of Laravel Core

Updated 3 months ago

Bhumi

ECMAScript 6 New Features – 2

Updated 1 year ago

Bhumi

The Reader’s Poll – June 2015

Posted on 4 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 5 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 6 years ago

Bhumi