Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

PHP

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

function printCompanyName($Company1, $Company2, $Company3)


{
echo “<p>$Company1</p>”;
echo “<p>$Company2</p>”;
echo “<p>$Company3</p>”;
}

PHP Functions 4
Calling Functions

function printCompanyName($CompanyName) {
echo “<p>$CompanyName</p>”;
}
printCompanyName(“Course Technology”);

Figure 4-1 Output of a call to a custom function

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 includes various predefined global


arrays, called autoglobals or superglobals
 Autoglobals contain client, server, and
environment information that you can use in
your scripts
 Autoglobals are associative arrays – arrays
whose elements are referred to with an
alphanumeric key instead of an index number

PHP Functions 8
Using Autoglobals (continued)

Table 4-1 PHP autoglobals

PHP Functions 9
Using Autoglobals (continued)

 Use the global keyword to declare a global


variable within the scope of a function
 Use the $GLOBALS autoglobal to refer to the
global version of a variable from inside a
function
 $_GET is the default method for submitting a
form
 $_GET and $_POST allow you to access the
values of forms that are submitted to a PHP
script

PHP Functions 10
Using Autoglobals (continued)

 $_GET appends form data as one long string


to the URL specified by the action attribute
 $_POST sends form data as a transmission
separate from the URL specified by the
action attribute

PHP Functions 11
Example #1

<?php
function writeMsg() {
echo "Hello world!";
} Hello world!

writeMsg(); // call the function


?>

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() {

echo "You are really a nice person, Have a nice time!";


}
/* Calling a PHP Function */

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.

• The POST method does not have any restriction on data


size to be sent.
• The POST method can be used to send ASCII as well as
binary data.
• The data sent by POST method goes through HTTP header
so security depends on HTTP protocol. By using Secure
HTTP you can make sure that your information is secure.
• The PHP provides $_POST associative array to access all
the sent information using POST method.

PHP Functions 19
PHP Functions 20

You might also like