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:

EXAMPLE:

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:

EXAMPLE:

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:

Generally we are calling a function like

function func_name(parameters)

so, with this We can call the function like

EXAMPLE:

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:

EXAMPLE:

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:

EXAMPLE:

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:

EXAMPLE:

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:

EXAMPLE:

OUTPUT:

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

How to use media upload in WordPress form?

Posted on 5 years ago

Bhumi

How to upload image in CodeIgniter?

Posted on 5 years ago

Bhumi