Lec # 21 PHP - IV
Lec # 21 PHP - IV
Lecture 21
User-Defined Functions
(PHP - IV)
1
FUNCTIONS
• In any programming language, it is ability to define
and use functions. The code you want to reuse.
• Format of functions:
function name($arguments) {
statements;
}
• We are talking about User-Defined functions
are functions we define ourselves.
• <?php
function say_hello($greeting, $target, $punct);
echo $greeting . “, ” . $target . $punct . “<br/>”;
$name = “John Doe”;
say_hello(“Greetings”, $name, “!”)
?>
EXAMPLE
• <?php
function addition($val1, $val2) {
$sum = $val1 + $val2;
}
echo addition(3, 4);
?>
Return Values
• We call the function, return the $sum value
in new variable.
• <?php
function addition($val1, $val2) {
$sum = $val1 + $val2;
return $sum;
}
$new_val = addition(3, 4);
echo $new_val;
?>
EXAMPLE
• <?php
function addition($val1, $val2) {
$sum = $val1 + $val2;
return $sum;
}
$new_val = addition(3, 4);
echo $new_val;
if (addition(5, 6) == 11) {
echo “Yes”;
}
?>
NOTE
• Make sure you are doing something with
return values.