Functions in PHP:

Functions are very important part of any language. In PHP also functions are very important. There are 1000 of built-in functions in PHP. Means PHP have many predefined functions.

A function is noting but a block of code which will be executed when we call that function wherever we need it. Functions save a lot of time because you can create a function for one time and you can use it for multiple times.

The working of function is very simple; it takes one or more inputs as parameters perform some operation and than returns the value back.

In PHP there are so many built-in functions are available. But you can also create your function and use then in your programming. First of all you have to create a function and than you have to call that function.

Note:

  • The name of function should start with a character or an underscore never start function name with number.
  • Do not use a predefine function name as your function name.
  • The names of function are not case sensitive.

 Creating User Define Function:

User can create his own function. To create a function you have to use “function” keyword.

Syntax:

 Example:

 Functions with Parameters/Argument:

Arguments are in function works just like a variable. Arguments in functions are used to pass information. Parameters are passed in function within the parentheses ‘()’ and you can pass “n” number of arguments in a function by separating them with commas (,).

Example:

            In following example we are using one argument.

 Example:

            In following example we are using two argument.

 The Default Argument Value in PHP Function:

If you define a argument value in function than when you do not declare it at the time of calling so it will take the default value of the argument.

Example:

In following example we are assigning value to the parameter and calling that function with argument and also without argument

 The Return Statement in PHP function:

If you want that the function returns some value so that you have to use “return” statement. You can also return more than value using return array.

Example:

 

Leave a Reply

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