PHP Chapter 5
PHP Chapter 5
Development
ITSE 3302
تطوير تطبيقات الشبكة العالمية
Dr. Moamar Elyazgi
PHP Language
Chapter 5
PHP
PHP ─ Functions
5.1 Introduction
PHP functions are similar to other programming languages. A function is a piece of
code which takes one more input in the form of parameter and does some
processing and returns a value.
The function is a self-contained block of statements that can repeatedly be
executed whenever we need it.
PHP has more than 1000 built-in functions, and in addition you can create your
own custom functions.
There are two types of functions as: Internal (built-in) Functions and User Defined
3
Functions
5.1 Introduction
o PHP Built-in Functions PHP User Defined Functions
o It has as over 1000 built-in functions that can o Besides the built-in PHP functions, it
be called directly, from within a script, to is possible to create your own
perform a specific task. functions.
https://www.php.net/manual/en/indexes.func o A function is a block of statements that
tions.php can be used repeatedly in a program.
o phpinfo(),print(), mysqli_connect(), o A function will not execute
error_reporting(), error_log(), array(). automatically when a page loads.
copy(), unlink(), date(), time(), strlen(), o A function will be executed by a call to
4
strlen(). the function.
5.1 Introduction
Some (not even most) usage of built- o Declaring and using an array
o Handling files
in functions are as mentioned
o Accessing data in forms
below:
o Filesystem Functions
o Converting a string of letters to
o Function to open FTP connections
uppercase and lowercase
o Email related functions
o Displaying and using the date and
o Mathematical Functions
time o MySQL specific functions
o Initializing and closing a database o URL Functions
5
connection o Image functions
5.2 Creating PHP Function
It is very easy to create your own PHP function. Suppose you want to create a PHP
function which will simply write a simple message on your browser when you will call it.
The following example creates a function called writeMessage() and then calls it just
after creating it.
Note that while creating a function its name should start with keyword function and all
the PHP code should be put inside { and } braces as shown in the following example
below:
function functionName()
{
Syntax code to be executed;
} 6
5.2 Creating PHP Function
<html> writeMessage();
<head> ?>
<title>Writing PHP Function</title> </body>
</head> </html>
<body>
<?php This will display the following result:
/* Defining a PHP Function */ You are really a nice person, Have a nice time!
function writeMessage()
{
echo "You are really a nice person, Have a nice time!";
}
/* Calling a PHP Function */ 7
5.3 PHP Functions with Parameters
PHP gives you option to pass your <?php
function familyName($fname, $year) {
parameters inside a function. You echo "$fname Refsnes. Born in $year <br>";
}
can pass as many as parameters
familyName("Hege","1975");
your like. familyName("Stale","1978");
familyName("Kai Jim","1983");
These parameters work like ?>
variables inside your function. The </body>
</html>
following example takes two Hege Refsnes. Born in 1975
Stale Refsnes. Born in 1978
integer parameters and adds them Kai Jim Refsnes. Born in 1983
<?php
function square ($num) {
return $num * $num;
}
// 16 الناتج هو
echo square (4);
?>
13
5.6 Dynamic Function Calls
It is possible to assign function <?php
function sayHello()
names as strings to variables and
{
then treat these variables exactly as echo "Hello<br />";
you would the function name itself.
}
$function_holder = "sayHello";
The following example depicts this $function_holder();
behavior: ?>