PHP- Functions

Functions contain a set of statements which are execute only after the function call. Function is called using the name of that function.

PHP User defined functions

  • Functions are basically used to run a particular set of code repeatedly.
  • The functions are execute only after the function call.
  • Name of the user defined functions are specified by the user.
  • ‘function’ is the first word used to define a function in php and then we declare name for that function.
  • The function start with a open curly brace and end with closed curly brace.
  • Under score or alphabet are used as starting letter of a function name.
  • For executing a function we must call that function using its name. So the function name is an important part of user defined function.
  • There no need of initialization before the function is used.

Syntax [ user defined function ]


Example 1 [ user defined function ]



Example 2 [ user defined function ]



PHP Function arguments

  • Arguments are like variables because we can store information with in an argument.
  • Arguments are normally used for passing information to function.
  • Arguments are specified with $ symbol followed by argument name.
  • There is an option for storing more than one arguments on a single function.
  • Arguments are specified with in open parenthesis of function name.

Syntax [ function arguments ]


Example 1 [ function arguments ]



Example 2 [ function arguments with two arguments ]



In some cases arguments store as a default value.

Example 3 [ function arguments with default argument ]



Returning values of PHP

The functions return value when we use some statements these statements are are called return statements. Basically return values are optional. We can display return statements in two ways both are shown in the given examples.


Example 1 [ returning values ]



Example 2 [ returning values ]



Variable Functions

The variable functions are mainly used to callback methods. We can assign a function name to any variable and then we can call the variable with the parenthesis. The function still executes. Let me explain it.


What is the expected output when we call the $temp variable without parenthesis. see the example.

Example 1 [ without parenthesis ] 



We can see how it work when the parenthesis append to it.

Example 2 [ variable functions ]




PHP- Functions
Article Name
PHP- Functions
Php functions tutorial about user defined functions, variable functions, how return values and arguments perform in functions.
Publisher Name
Jees K Denny
Publisher Logo

Leave a Reply