Function in PHP:
Function is a part of program that are used to perform some specific task.
program:
<?php
function test()
echo "HELLO-PHP";
test();
?>
function with argument:
<?php
function test($name)
echo "HELLO $name";
test("khushbu");
?>
Program to calculate square of a number:
<?php
function square($num)
return $num*$num;
echo square(5);
?>
Program to calculate cube of a number:
<?php
function cube($num)
return $num*$num*$num;
}
echo cube(5);
?>
Pass two arguments with function:
<?php
function student($name, $roll)
echo "Student name: $name <BR>";
echo "Roll number: $roll <BR> ";
student("ABC","1");
student("XYZ","2");
?>
Call by reference
<?php
//call by reference
function add(&$x)
{
$x= $x+1;
}
$num=5;
add($num);
echo "number= $num <br>";
?>