Professional Documents
Culture Documents
Lect 5
Lect 5
Functions
Lecture # 5
Defining Functions
Functions are groups of statements that you
can execute as a single unit
Function definitions are the lines of code that
make up a function
The syntax for defining a function is:
<?php
function name_of_function(parameters) {
statements;
}
?>
PHP Functions 2
Defining Functions (continued)
Functions, like all PHP code, must be
contained within <?php ... ?> tags
A parameter is a variable that is used within a
function
Parameters are placed within the parentheses
that follow the function name
Functions do not have to contain parameters
The set of curly braces (called function
braces) contain the function statements
PHP Functions 3
Defining Functions (continued)
Function statements do the actual work of
the function and must be contained within the
function braces
PHP Functions 4
Calling Functions
function printCompanyName($CompanyName) {
echo “<p>$CompanyName</p>”;
}
printCompanyName(“Course Technology”);
PHP Functions 5
Returning Values
A return statement is a statement that returns
a value to the statement that called the
function
A function does not necessarily have to return
a value
function averageNumbers($a, $b, $c) {
$SumOfNumbers = $a + $b + $c;
$Result = $SumOfNumbers / 3;
Return $Result;
}
PHP Functions 6
Understanding Variable Scope
Variable scope is where in your program a
declared variable can be used
A variable’s scope can be either global or local
A global variable is one that is declared
outside a function and is available to all parts
of your program
A local variable is declared inside a function
and is only available within the function in
which it is declared
PHP Functions 7
Using Autoglobals
PHP Functions 8
Using Autoglobals (continued)
PHP Functions 9
Using Autoglobals (continued)
PHP Functions 10
Using Autoglobals (continued)
PHP Functions 11
Example #1
<?php
function writeMsg() {
echo "Hello world!";
} Hello world!
PHP Functions 12
<!DOCTYPE html>
<html>
<body>
<?php
function familyName($fname) {
echo "$fname Refsnes.<br>"; Jani Refsnes.
} Hege Refsnes.
Stale Refsnes.
familyName("Jani"); Kai Jim Refsnes.
familyName("Hege"); Borge Refsnes.
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
?>
</body>
</html>
PHP Functions 13
<html>
<head>
<title> Writing PHP Function </title>
</head>
<body>
<?php
You are really a nice
/* Defining a PHP Function */ person, Have a nice
time!
function writeMessage() {
writeMessage();
?>
</body>
</html>
PHP Functions 14
PHP Functions 15
The GET Method
The GET method sends the encoded user
information appended to the page request. The
page and the encoded information are
separated by the ? character.
PHP Functions 16
The GET Method
The GET method produces a long string that appears in your server
logs, in the browser's Location: box.
The GET method is restricted to send upto 1024 characters only.
Never use GET method if you have password or other sensitive
information to be sent to the server.
GET can't be used to send binary data, like images or word
documents, to the server.
The data sent by GET method can be accessed using
QUERY_STRING environment variable.
The PHP provides $_GET associative array to access all the sent
information using GET method.
PHP Functions 17
PHP Functions 18
The POST Method
The POST method transfers information via HTTP headers. The
information is encoded as described in case of GET method and put
into a header called QUERY_STRING.
PHP Functions 19
PHP Functions 20