Function Advantages:
- You reduce code duplication.
- You can reuse code across scripts
- You can write complex code more easily
- You can reduce errors in your code
There is more..
To Create Functions:
to make function is simple, here is the syntax for that,
function functionName()
{
code to be executed;
}
Simple Eg,
<!--?php
function writeName()
{
echo "niyaz";
}
echo "My name is ";
writeName();
?>
PHP Functions - Adding parameters
To add more functionality to a function, we can add parameters. A parameter is just like a variable.
<!--?php
function writeName($fname)
{
echo $fname;
}
echo "My name is ";
writeName("niyaz");
echo "My sister's name is ";
writeName("fouz");
echo "My brother's name is ";
writeName("Azar");
?>
PHP Functions - Return values
letting functions to return values, to do that
follow the below example.
<!--?php
function add($x,$y)
{
$total=$x+$y;
return $total;
}
echo "1 + 16 = " . add(1,16);
?>
So this is what we can be used functions in PHP. All programming languages use this concept, functions are playing very important role when your create applications. so just practice with these functions some hours. This will be simple.
Post you comments.
Post a Comment