New way to learn PHP

PHP 5 Function

What is function?

Function is a self - contained block of code that performs a coherent task, that's assigned a unique name. It's also called subprogram and you can call that function from else anywhere in your script.

Some important point about function

  • A function optionally accepts one or more arguments.
  • The function can call any number of times in a program.
  • A function can pass a value back, where function is called.
  • Functions get provide the facility to develop reusable code and easily modified.

Function example

function firstfun() 

echo "Hello !". "
"; echo date("Y/m/d") . "
"; echo " Welcome to the function "."
"; } firstfun();//calling of function firstfun(); firstfun(); ?>

Firstfun is a function that is printing date and some statement, defined once, but called many times as shown in the above.

Advantages of function?

  • Reusability of code
  • The function gets remove the duplicate code problem.
  • Function make it easier to reduce errors.
  • You can be reused functions in other scripts.
  • Function help to break your big script into small scripts.

Types of function

Function are mainly two types in PHP

  1. User define function
  2. Pre-define function

User define function

The User define functions are those function are getting made by programmer that perform a coherent task.

  • The advantage of user-defined function is the code reusability
  • Easily pass the arguments

Syntax of function declaration

Function function-name()
Code to be executed...

Syntax of function calling


Example declaration and calling


function difference() 
 $first = 35;
 $second = 30;
 $diff = $first - $second;
 echo "Difference = ".$diff."
"; } difference(); function add() { $first = 15; $second = 30; $sum = $first + $second; echo "Sum = ".$sum."
"; } add(); difference(); /*you can call function any where in your program */ add(); ?>

In the above example, add and difference are the user-defined function, that are called more than one time.

Note: You can call the function any number of times and anywhere in your PHP script.

Another example of function


function difference() 
 $first = 35;
 $second = 30;
 $diff = $first - $second;
 echo "Difference = ".$diff."
"; } function add() { difference(); //you can call function within function $first = 15; $second = 30; $sum = $first + $second; echo "Sum = ".$sum."
"; } add() //only add() called ?>

In the above example, add and difference are the function and difference will be called when add will be called.

You can call the function within the function as shown in the above example

Only Add() called and difference() automatically called

Function with arguments

We know functions can optionally accept one or more than one arguments, which are values passed to the function. To tell PHP that you want your function to accept arguments, a parameter or argument is a variable that holds the value passed to it when the function is called.

One argument example


function car($name) 
	echo "Name of cars"."
"; echo "name:".$name."
"; } car("BMW"); car("AUDI"); car("MERCEDEZ"); ?>

In the above example, you must pass one variable or you can say argument at the time of calling the function otherwise "Notice: Undefined variable: name" will come.

Multiple argument example

echo "Marks of student "."
"; function student($name,$a,$b,$c) { $sum=$a+$b+$c; echo"Name:".$name."
"; echo"Total marks=$sum"."
"; } student("john",85,95,75); student("Smith",84,72,52); student("Brown",75,84,52); ?>

You can pass multiple arguments as well.

Returning Values from Your Functions

You can take value from the function by using return keyword. Return keyword is the only way to take value from the function. Find Square Using Return Keyword

Find Square using return keyword


function square($no) 

return ($no*$no);


echo  "square=".square(5)."
"; echo "square=".square(10)."
"; echo "square=".square(12.25)."
"; echo "square=".square(572)."
"; ?>

In the above example, square is a user-defined function that takes one argument and returns square of this number.

Recursive Functions

A type of function that's calling itself again and again. A function is known as recursive if a statement within the body of the function calls the same function.

Find factorial using recursion


function fact($no) 
  if( $no == 1)
	return 1;
echo "factorial = ". fact(4);

In the above example, fact is a recursive function that will be called itself until $no = 1.


Wonder is the first cause of philosophy.