PHP FUNCTIONS
By
S.Abinaya
13N103
Besides
the built-in PHP functions, we can create our
own functions
A function is a block of statements that can be used
repeatedly in a program
A function will not execute immediately when a page
loads
A function will be executed by a call to the function
PHP USER DEFINED FUNCTIONS
A user
defined function declaration starts with the
word "function":
SYNTAX
function functionName() {
codetobeexecuted;
}
CREATE A USER DEFINED
FUNCTION IN PHP
<?php
function writeMsg() {
echo "Hello world!";
}
writeMsg(); // call the function
?>
EXAMPLE
Information
can be passed to functions through
arguments. An argument is just like a variable.
Arguments are specified after the function name,
inside the parentheses. You can add as many
arguments as you want, just separate them with a
comma.
PHP FUNCTION ARGUMENTS
<?php
function familyName($fname) {
echo "$fname Refsnes.<br>";
}
familyName("Jani");
familyName(Krishna");
?>
EXAMPLE
<?php
function familyName($fname, $year) {
echo "$fname Refsnes. Born in $year <br>";
}
familyName(Udhaya", "1991");
familyName(Jani", "1996");
?>
EXAMPLE With two arguements
<?php
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight(); // will use the default value of 50
?>
PHP DEFAULT ARGUMENT VALUE
<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}
echo "5 + 10 = " . sum(5, 10) . "<br>";
echo "7 + 13 = " . sum(7, 13) . "<br>";
echo "2 + 4 = " . sum(2, 4);
?>
PHP FUNCTIONS - RETURNING
VALUES
THANK YOU