Professional Documents
Culture Documents
FUNCTIONS
EXAMPLE
A simple function that writes my name when it is called:
<html> <body> <?php function writeMyName() { echo "Kai Jim Refsnes"; } writeMyName(); ?> </body> </html>
OUTPUT
Hello world! My name is Munirah. That's right, Munirah is my name.
ADDING PARAMETERS
Our first function (writeMyName()) is a very simple function. It only writes a static string. To add more functionality to a function, we can add parameters. A parameter is just like a variable. You may have noticed the parentheses after the function name, like: writeMyName(). The parameters are specified inside the parentheses.
EXAMPLE
The following example will write different first names, but the same last name:
<html> <body> <?php function writeMyName($fname) { echo $fname . " Abdullah.<br />"; } echo "My name is ";writeMyName(Munirah"); echo "My name is ";writeMyName(Mahirah"); echo "My name is ";writeMyName(Mardhiah"); echo "My name is ";writeMyName(Masturah"); echo "My name is ";writeMyName(Muhammad Zaim");
?> </body> </html>
OUTPUT
My My My My My
is is is is is
Munirah Abdullah. Mahirah Abdullah. Mardhiah Abdullah. Masturah Abdullah. Muhammad Zaim Abdullah.
OUTPUT
My My My My My
is is is is is
Munirah Abdullah. Mahirah Abdullah! Mardhiah Abdullah... Masturah Abdullah!.. Muhammad Zaim Abdullah??
RETURN VALUES
<html> <body> <?php function add($x,$y) { $total = $x + $y; return $total; } echo "1 + 16 = " . add(1,16) ?> </body> </html>
1 + 16 = 17
Output
THE END