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.

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

stringcreate_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

boolfunction_exists(string$function_name)

EXAMPLE:

1

2

3

4

5

6

7

8

9

10

11

12

functionfunc_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.

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

functionBox($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

intfunc_num_args(void)

EXAMPLE:

1

2

3

4

5

6

7

8

9

functioncombined()

{

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

Post navigation

About Author

Bhumi

Bhumi shah is currently working with leading web development company as a Software Analyst and also the founder of the Creativedev. She has immense interest in programming and web designing.She is passionate about technical blogging and almost versatile in terms of programming across various languages & frameworks such as PHP,MySQL WordPress, Twitter Bootstrap, AngularJS, HTML5, CSS3, jQuery and more.