Professional Documents
Culture Documents
PHP Functions
Continued..
A function name can start with a letter or
underscore (not a number).
Give the function a name that reflects what
the function does.
Function names are NOT case-sensitive.
Example
<?php
function writeMsg() {
echo Welcome to PHP Page!";
}
writeMsg(); // call the function
?>
OutPut
Welcome to PHP Page
Continued..
The following example has a function with one
argument ($fname). When the familyName() function is
called, we also pass along a name (e.g.Ali), and the
name is used inside the function, which outputs several
different first names, but an equal last name:
<?php
function familyName($fname) {
echo "$fname Ahmed.<br>";
}
familyName(Ali");
familyName(Usman");
familyName(Hafeez");
?>
Continued..
Output:
Ali Ahmed
Usman Ahmed
Hafeez Ahmed
Example
<?php
function familyName($fname, $year) {
echo "$fname Ahmed. Born in $year <br>";
}
familyName(Ali", "1975");
familyName(Usman", "1978");
familyName(Hafeez", "1983");
?>
Continued..
Output:
Ali Ahmed. Born in 1975
Usman Ahmed. Born in 1978
Hafeez Ahmed. Born in 1983
Continued..
Output :
The height is : 350
The height is : 50
The height is : 135
Continued..
Output:
5 + 10 = 15
7 + 13 = 20
Calling a Function
Dynamically
<?php
function sayHello()
{
echo "hello<br>";
}
$function_holder = "sayHello";
$function_holder();
?>
<?php
function addFive( $num )
{
$num += 5;
}
$orignum = 10;
addFive( $orignum );
print( $orignum );
?>
Example
<?php
srand( microtime() * 1000000 );
$Rad=rand(1,10);
echo $Rad;
?>