Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP Function and Array 05 CHAPTER Padasalai MOHAMED FAKRUDEEN

Similar presentations


Presentation on theme: "PHP Function and Array 05 CHAPTER Padasalai MOHAMED FAKRUDEEN"— Presentation transcript:

1 PHP Function and Array 05 CHAPTER Padasalai MOHAMED FAKRUDEEN
PGT-COMPUTER SCIENCE SHAMROCK MATRIC. HR. SEC. SCHOOL MOGAPPAIR, CHENNAI-107

2 MOHAMED FAKRUDEEN 7/7/2019 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 FAKRUDEEN 7/7/2019 Functions in PHP

4 MOHAMED FAKRUDEEN 7/7/2019 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 What is Function? (Cont…)
MOHAMED FAKRUDEEN 7/7/2019 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 FAKRUDEEN 7/7/2019 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 FAKRUDEEN 7/7/2019 User defined Function

8 MOHAMED FAKRUDEEN 7/7/2019 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 FAKRUDEEN 7/7/2019 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 FAKRUDEEN 7/7/2019 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 Example - Program <?php function insertMsg() {
MOHAMED FAKRUDEEN 7/7/2019 Example - Program <?php function insertMsg() { echo “Student Details Inserted Successfully!”; } insertMsg(); // call the function ?>

12 Parameterized Function
MOHAMED FAKRUDEEN 7/7/2019 Parameterized Function

13 Parameterized Function
MOHAMED FAKRUDEEN 7/7/2019 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 FAKRUDEEN 7/7/2019 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 FAKRUDEEN 7/7/2019 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 FAKRUDEEN 7/7/2019 Example - Program3 For a function to return a value, use the return statement <?php function sum($x, $y) { $z = $x + $y; return $z; } echo “ = “ . sum(5, 10) . “<br>”; echo “ = “ . sum(7, 13) . “<br>”; echo “2 + 4 = “ . sum(2, 4); ?>

17 MOHAMED FAKRUDEEN 7/7/2019 ARRAYS IN PHP

18 MOHAMED FAKRUDEEN 7/7/2019 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 FAKRUDEEN 7/7/2019 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 Indexed Arrays Array Syntax: Array defines with the keyword array()
MOHAMED FAKRUDEEN 7/7/2019 Indexed Arrays Array Syntax: Array defines with the keyword array()

21 Example: <?php $teacher_name=array(“Iniyan”, “Kavin”, “Nilani”);
MOHAMED FAKRUDEEN 7/7/2019 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 FAKRUDEEN 7/7/2019 Associative Arrays 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 Syntax array(key=>value,key=>value,key=>value,etc.);
MOHAMED FAKRUDEEN 7/7/2019 Syntax array(key=>value,key=>value,key=>value,etc.); key = Specifies the key (numeric or string) value = Specifies the value

24 MOHAMED FAKRUDEEN 7/7/2019 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 Multidimensional Arrays
MOHAMED FAKRUDEEN 7/7/2019 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 <?php// A two-dimensional array $student=array (
MOHAMED FAKRUDEEN 7/7/2019 <?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] echo $$student[2][0].”: Tamil Mark: “.$student [2][1].”. English mark: “.$student [2] ?>

27 POINTS TO REMEMBER PHP Functions are Reducing duplication of code
MOHAMED FAKRUDEEN 7/7/2019 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 CONDITIONAL STATEMENTS
MOHAMED FAKRUDEEN 7/7/2019 NEXT TOPIC: CONDITIONAL STATEMENTS


Download ppt "PHP Function and Array 05 CHAPTER Padasalai MOHAMED FAKRUDEEN"

Similar presentations


Ads by Google