PHP Functions


 

PHP Functions

Function provides power to any programming language. It works like a small machine to perform any task between the programs.

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.

Syntax

function functionName()
{
code to be executed;
}

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

writeName();
?>

</body>
</html>

Output:
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:
<?php
function RoseIndia()
{
print "<b>This is Rose India!</b>";
}
RoseIndia();
?>

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:
<?php
function writeName($fname)
{
echo $fname . “ Kumar.<br />";
}

 echo “My name is ” ;
writeName(“Deepak”);
echo “My brother’s name is ”;
writeName(“Amit”);
echo “My friend’s name is ”;
writeName(“Ravi”);
?>

Output:
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.

Example

<?php
function add($A,$B)
{
$total=$A+$B;
return $total;
}
echo “1 + 16 = ” . add(1,16);
?>

Output:
1 + 16 = 17

Ads