Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

How to Define and Call a Function in PHP Complete Tutorial

How to Define and Call a Function in PHP Complete Tutorial

PHP function is a set of specific program and source code which complete a specific task is function.
Function separates the complexity of a large program and provides a modular approach. There are two type of function user define function and library function.  Now focus on user defines function and try to understand.
Syntax of function:
function   <function name>  (parameter list)
{
body of function;

}

Type of PHP Functions

Category 1:         function with no argument and no return values.
Category 2:         function with argument and no return value.
Category 3:         function with arguments and one return value.
pro1.php

In this example 40 times star print. Now this task  is performed with the help of function which is in next example pro2.php.
<?php
echo”****************************************”.”<br>”;
echo”****************************************”.”<br>”;

?>

PHP Function with no argument and no return value

pro2.php
<?php
function star()             //no parameter
{
for($i=1;$i<=40;$i++)
{
echo “*”;         //no return  value
}
echo”<br>”;
}
star();                        //function call but no argument
star();

?>

PHP Function with one argument but no return value

pro3.php
<?php
function reverse($num)         //parameter receive
{
for($mod=0,$rev=0;$num>0;$num=(int)($num/10))
{
$mod=$num%10;
$rev=$rev*10+$mod;
}
echo “Out put in function”.”<br>”;
echo “Reverse=”.$rev;
//function no return .
}

 

$number=365;
reverse($number);              //argument passing
?>

PHP Function with arguments and return value

pro4.php
<?php
function reverse($num)                //parameter  list
{
for($mod=0,$rev=0;$num>0;$num=(int)($num/10))
{
$mod=$num%10;
$rev=$rev*10+$mod;
}

return($rev);                       // function return a value.

}

$number=365;
$result=reverse($number);             //argument passing
echo “Out put out of function”.”<br>”;
echo “Reverse=”.$result;

?>

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments