Home Tutorial Php Phpbeginners PHP Functions


PHP Functions
Posted on: July 10, 2009 at 12:00 AM
Function provides power to any programming language. It works like a small machine to perform any task between the programs.

3.11. Functions

Function provides power to any programming language. It works like a small machine to perform any task between the programs. In PHP, Function is the real power which can be created and utilized as per the programming need. Moreover, PHP has its own built-in canned function library that can be used for performing a wide range of services like from sorting stuff in alphabetical order to sending email, from connecting with databases to balancing our spaceship’s inertial sub-space dampers.
Here, you will learn ‘how to made your own functions?’

3.11.1. Create a PHP Function

A function will be executed by a call to the function. A function is created by giving a name that reflects what the function does. The name of the function begins with a letter or underscore not with a number.


function functionName()
code to be executed;

Here we will print my name through using function.
function writeName()
Print “Deepak Kumar”;
Print “My name is”;



My name is Deepak Kumar.
So you begin your with the word WriteName (), define the rules of the function inside the following curly brackets {}. Call it within the program and execute it. It works according to the rule set by you.
There are two types of functions available: one that requires ‘argument’ and second that does not. An argument is a variable that comes from outside the function, but which the function needs in order to run.
First see an example of function without argument:
function RoseIndia()
print "<b>This is Rose India!</b>";

The output is:
This is Rose India.

See how it works:

  • start PHP
  • create function RoseIndia
  • start definition of function RoseIndia
  • definition of RoseIndia is to print “This is RoseIndia!” inside and tags
  • end definition of RoseIndia
  • call function RoseIndia (meaning “do the thing that we defined the function to do”)
  • close PHP

As you have seen that the defined function inside the <?php and ?>tags can be called any times and any where in the program. Suppose if you will have to print it thousands of time through simple coding, it was very hectic and time consuming.
Now move ahead towards functions with parameter. Parameter provides function more feathers to perform more complex works. Parameters are specified after the function name, inside the parentheses. See an example:
The following example will write different first names, but equal last name:
function writeName($fname)
echo $fname . “ Kumar.<br />";

 echo “My name is ” ;
echo “My brother’s name is ”;
echo “My friend’s name is ”;

My name is Deepak Kumar
My sister’s name is Amit Kumar
My brother's name is Ravi Kumar

PHP Functions - Return values

The return() statement immediately ends execution of the current function, and returns its argument as the value of the function call. return() will also end the execution of an eval() statement or script file, e.g. see an example of addition of two numbers.


function add($A,$B)
return $total;
echo “1 + 16 = ” . add(1,16);

1 + 16 = 17


Related Tags for PHP Functions:

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 10, 2009

Recommend the tutorial

Advertisements Advertisements