0% found this document useful (0 votes)
124 views13 pages

F5224 Web Programming: Functions

Functions allow you to reuse blocks of code in PHP. A function is defined using the function keyword followed by parentheses containing any parameters. The code block is wrapped in curly braces. Functions can optionally return values using the return statement. Parameters allow functions to accept input which makes them more flexible and reusable for different inputs. Functions improve code organization and reuse.

Uploaded by

Iwan Saputra
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views13 pages

F5224 Web Programming: Functions

Functions allow you to reuse blocks of code in PHP. A function is defined using the function keyword followed by parentheses containing any parameters. The code block is wrapped in curly braces. Functions can optionally return values using the return statement. Parameters allow functions to accept input which makes them more flexible and reusable for different inputs. Functions improve code organization and reuse.

Uploaded by

Iwan Saputra
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

F5224 WEB PROGRAMMING

FUNCTIONS

WHAT IS PHP FUNCTION?


A function is a block of code that can be executed whenever we need it.

CREATING PHP FUNCTIONS


Creating PHP functions:
All functions start with the word "function()" Name the function - It should be possible to understand what the function does by its name. The name can start with a letter or underscore (not a number) Add a "{" - The function code starts after the opening curly brace Insert the function code Add a "}" - The function is finished by a closing curly brace

EXAMPLE
A simple function that writes my name when it is called:
<html> <body> <?php function writeMyName() { echo "Kai Jim Refsnes"; } writeMyName(); ?> </body> </html>

USE A PHP FUNCTION


<html> <body> <?php function writeMyName() { echo "Munirah"; } echo "Hello world!<br />"; echo "My name is "; writeMyName(); echo ".<br />That's right, "; writeMyName(); echo " is my name."; ?> </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

name name name name name

is is is is is

Munirah Abdullah. Mahirah Abdullah. Mardhiah Abdullah. Masturah Abdullah. Muhammad Zaim Abdullah.

EXAMPLE ADDING TWO(2) PARAMETERS


<html> <body> <?php function writeMyName($fname,$punctuation) { echo $fname . " Abdullah" . $punctuation . "<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

name name name name name

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

You might also like