Xii Ca Chapter 05 PHP Function and Array
Xii Ca Chapter 05 PHP Function and Array
MOHAMED FAKRUDEEN
PGT-COMPUTER SCIENCE
SHAMROCK MATRIC. HR. SEC. SCHOOL
MOGAPPAIR, CHENNAI-107
2
MOHAMED 6/3/2019
FAKRUDEEN
LEARNING OBJECTIVES
• To understand the importance of Functions
concept in PHP
MOHAMED 6/3/2019
FAKRUDEEN
4
MOHAMED 6/3/2019
FAKRUDEEN
What is Function?
• In most of the programming language, a block of
segment in a program that performs a specific
operation tasks such as
▫ Insert
▫ Execute
▫ Delete
▫ Calculate etc.
• This segment is also known as Function.
5
MOHAMED 6/3/2019
FAKRUDEEN
MOHAMED 6/3/2019
FAKRUDEEN
Types of functions
• The Function can be divided in to three types as
follows
▫ User defined Function
▫ Pre-defined or System or built-in Function
▫ Parameterized Function.
7
MOHAMED 6/3/2019
FAKRUDEEN
MOHAMED 6/3/2019
FAKRUDEEN
MOHAMED 6/3/2019
FAKRUDEEN
Function Declaration
MOHAMED 6/3/2019
FAKRUDEEN
Function Calling
• A function declaration part will be executed by a
call to the function.
• Programmer has to create Function Calling part
inside the respective program.
• Syntax:
▫ functionName();
11
MOHAMED 6/3/2019
FAKRUDEEN
Example - Program
<?php
function insertMsg()
{
echo “Student Details Inserted Successfully!”;
}
insertMsg(); // call the function
?>
12
MOHAMED 6/3/2019
FAKRUDEEN
Parameterized Function
13
MOHAMED 6/3/2019
FAKRUDEEN
Parameterized Function
• Required information can be shared between
function declaration and function calling part
inside the program.
• The parameter is also called as arguments, it is
like variables.
• The arguments are mentioned after the function
name and inside of the parenthesis.
• There is no limit for sending arguments, just
separate them with a comma notation.
14
MOHAMED 6/3/2019
FAKRUDEEN
Example – Program 1
The following example has a function with one Argument ($sfname):
<?php
function School_Name($sname)
{
echo $sname.”in Tamilnadu.<br>”;
}
School_Name (“Government Higher Secondary School Madurai”);
School_Name (“Government Higher Secondary School Trichy”);
School_Name (“Government Higher Secondary School Chennai”);
School_Name (“Government Higher Secondary School
Kanchipuram”);
School_Name (“Government Higher Secondary School Tirunelveli”);
?>
15
MOHAMED 6/3/2019
FAKRUDEEN
Example – Program 2
The following example has a function with two arguments ($sname and $Strength):
<?php
function School_Name($sname,$Strength) {
echo $sname.”in Tamilnadu and Student Strength is”.$Strength;
}
School_Name (“Government Higher Secondary School Madurai”,200);
School_Name (“Government Higher Secondary School Trichy”,300);
School_Name (“Government Higher Secondary School Chennai”,250);
School_Name (“Government Higher Secondary School
Kanchipuram”,100);
School_Name (“Government Higher Secondary School
Tirunelveli”,200);
?>
16
MOHAMED 6/3/2019
FAKRUDEEN
Example - Program3
For a function to return a value, use the return statement
<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}
echo “5 + 10 = “ . sum(5, 10) . “<br>”;
echo “7 + 13 = “ . sum(7, 13) . “<br>”;
echo “2 + 4 = “ . sum(2, 4);
?>
17
MOHAMED 6/3/2019
FAKRUDEEN
ARRAYS IN PHP
18
MOHAMED 6/3/2019
FAKRUDEEN
MOHAMED 6/3/2019
FAKRUDEEN
Indexed Arrays
• Arrays with numeric index for the available
values in array variable which contains key
• value pair as user / developer can take the values
using keys.
20
MOHAMED 6/3/2019
FAKRUDEEN
Array Syntax:
• Array defines with the keyword array()
21
MOHAMED 6/3/2019
FAKRUDEEN
Example:
<?php
$teacher_name=array(“Iniyan”, “Kavin”,
“Nilani”);
echo “The students name are “ .
$teacher_name[0] . “, “ . $$teacher_name[1] . “
and “ .
$teacher_name[2] . “.”;
?>
22
MOHAMED 6/3/2019
FAKRUDEEN
MOHAMED 6/3/2019
FAKRUDEEN
array(key=>value,key=>value,key=>value,etc.);
MOHAMED 6/3/2019
FAKRUDEEN
Example:
<?php
$Marks=array(“Student1”=>“35”,“Student2”=>“1
7”,“Student3”=>“43”);
echo “Student1 mark is” . $Marks[‘Student1’] . “ is
eligible for qualification”;
echo “Student2 mark is” . $Marks[‘Student2’] . “ is
not eligible for qualification”;
?>
25
MOHAMED 6/3/2019
FAKRUDEEN
Multidimensional Arrays
• A multidimensional array is an array containing
one or more arrays.
• PHP understands multidimensional arrays that
are two, three, four, five, or more levels deep.
• However, arrays more than three levels deep are
hard to manage for most people.
26
MOHAMED 6/3/2019
FAKRUDEEN
MOHAMED 6/3/2019
FAKRUDEEN
POINTS TO REMEMBER
• PHP Functions are Reducing duplication of code
• PHP Functions are Decomposing complex problems
into simpler pieces
• PHP Functions are Improving clarity of the code
• PHP Functions are Reuse of code
• PHP Functions are Information hiding
• PHP arrays are using For each looping concepts
28
MOHAMED 6/3/2019
FAKRUDEEN