0% found this document useful (0 votes)
23 views15 pages

PHP Chapter 5

PHP-Chapter-5

Uploaded by

taj200153
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views15 pages

PHP Chapter 5

PHP-Chapter-5

Uploaded by

taj200153
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Web Applications

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

together and then prints them. 8


5.3 PHP Functions with Parameters
<html>
<head>
<title>Writing PHP Function with Parameters</title>
</head>
<body>
<?php
function addFunction($num1, $num2)
{
$sum = $num1 + $num2;
echo "Sum of the two numbers is : $sum";
}
addFunction(10, 20);
?> This will display the following result:
</body> Sum of the two numbers is : 30 9
</html>
5.4 Passing Arguments by Reference
 It is possible to pass arguments to functions by reference. This means that a
reference to the variable is manipulated by the function rather than a copy of
the variable's value.
 Any changes made to an argument in these cases will change the value of
the original variable.
 You can pass an argument by reference by adding an ampersand to the
variable name in either the function call or the function definition.
 The following example depicts both the cases.
10
5.4 Passing Arguments by Reference
<?php
function addFive($num) $orignum = 10;
{ addFive( &$orignum );
$num += 5; echo "Original Value is $orignum<br />";
} addSix( $orignum );
function addSix(&$num) echo "Original Value is $orignum<br />";
{ ?>
$num += 6;
}
This will display the following result:
Original Value is 10
Original Value is 16
11
5.5 PHP Functions Returning Value
 A function can return a value using the return statement in conjunction with a
value or object.
 return stops the execution of the function and sends the value back to the
calling code.
 You can return more than one value from a function using return array(1,2,3,4).
 The following example takes two integer parameters and add them together and
then returns their sum to the calling program.
 Note that return keyword is used to return a value from a function.
12
5.5 PHP Functions Returning Value

<?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: ?>

This will display the following result:


Hello
14
15

You might also like