0% found this document useful (0 votes)
155 views

Xii Ca Chapter 05 PHP Function and Array

This document provides an overview of functions and arrays in PHP. It defines what functions are, the different types of functions (user defined, predefined, parameterized), and provides examples of function declarations and calls. It also defines what arrays are, the different types of arrays (indexed, associative, multidimensional), and provides examples of creating and accessing values in each array type. The document is intended to help readers understand the basics of functions and arrays, which are important concepts in PHP programming.

Uploaded by

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

Xii Ca Chapter 05 PHP Function and Array

This document provides an overview of functions and arrays in PHP. It defines what functions are, the different types of functions (user defined, predefined, parameterized), and provides examples of function declarations and calls. It also defines what arrays are, the different types of arrays (indexed, associative, multidimensional), and provides examples of creating and accessing values in each array type. The document is intended to help readers understand the basics of functions and arrays, which are important concepts in PHP programming.

Uploaded by

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

Padasalai

PHP Function and Array


05 CHAPTER

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

• To know the types of functions concept in PHP

• To know the basics of array concepts

• To know the types of Arrays concept in PHP


3

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

What is Function? (Cont…)


• A Function is a type of sub routine or procedure
in a program.
• A Function will be executed by a call to the
Function and the Function returns any data type
values or NULL value to called Function in the
part of respective program
6

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

User defined Function


8

MOHAMED 6/3/2019
FAKRUDEEN

User Defined Function

• User Defined Function (UDF) in PHP gives a


privilege to user to write own specific operation
inside of existing program module.
• Two important steps the Programmer has to
create for users define Functions are:
▫ Function Declaration
▫ Function Calling
9

MOHAMED 6/3/2019
FAKRUDEEN

Function Declaration

• A user-defined Function declaration begins with


the keyword “function”.
• User can write any custom logic inside the
function block.
• Syntax:
function functionName()
{
Custom Logic code to be executed;
}
10

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

What are arrays?


• Array is a concept that stores more than one
value of same data type (homogeneous) in single
array variable.

• They are 3 types of array concepts in PHP.


▫ Indexed Arrays,
▫ Associative Array and
▫ Multi-Dimensional Array.
19

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

• Associative arrays are a key-value pair data


structure. Instead of having storing data in a
• linear array, with associative arrays you can
store your data in a collection and assign it a
• unique key which you may use for referencing
your data.
23

MOHAMED 6/3/2019
FAKRUDEEN

array(key=>value,key=>value,key=>value,etc.);

•key = Specifies the key (numeric or string)


•value = Specifies the value
24

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

<?php// A two-dimensional array


$student=array
(
array(“Iniyan”,100,96),
array(“Kavin”,60,59),
array(“Nilani”,1313,139)
• );
• echo $$student[0][0].”: Tamil Mark: “.$student [0][1].”.
English mark: “.$student [0]
• [2].”<br>”;
• echo $$student[1][0].”: Tamil Mark: “.$student [1][1].”.
English mark: “.$student [1]
• [2].”<br>”;
• echo $$student[2][0].”: Tamil Mark: “.$student [2][1].”.
English mark: “.$student [2]
• [2].”<br>”;
• ?>
27

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

You might also like