+ All Categories
Home > Documents > Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali...

Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali...

Date post: 06-Jun-2020
Category:
Upload: others
View: 7 times
Download: 0 times
Share this document with a friend
40
Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) [email protected] 1 Lecture 20
Transcript
Page 1: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

Introduction to PHP (Part-2)

Mr. Mubashir AliLecturer (Dept. of Computer Science)

[email protected]

1

Lecture 20

Page 2: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

Summary of the previous lecture

• Setting the environment

• Overview of PHP

• Constants and Variables in PHP

Mubashir Ali - Lecturer (Department of Computer Science).

2

Page 3: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

Outline

• Operators in PHP

• Conditional Statements in PHP

• Looping Statements

• Arrays in PHP

Mubashir Ali - Lecturer (Department of Computer Science).

3

Page 4: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

1. Operators in PHP

Mubashir Ali - Lecturer (Department of Computer Science).

4

• Arithmetic Operators:

– +, - ,*, /, %

• Assignment Operators:

– =

–+= ($a +=$b ), *= , /=

– .= ($a .= $b)

Page 5: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

5

Adds $b in $a

Concatenates $b with $a

Page 6: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

6

Page 7: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

7

• String Operators:

– . , .=

– $a=“abcd”.”efgh”; $a=abcdefgh

– $a.=“ijk”; $a=abcdefghijk

• Increment/decrement Operators:

– ++,--

–$b=$a++

–$b=++$a

Page 8: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

8

First variable

Second variable

Concatenation

Using .=

Page 9: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

9

Page 10: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

10

Variable declared

Incremented before display

Incremented after display

Displaying incremented value

Page 11: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

11

Page 12: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

12

• Logical Operators:

– AND, OR, NOT, XOR

– &&, ||, !

• Equality Operators:

– ==, !=, ===

• Comparison Operators:

– >, <, <=, >=

Page 13: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

13

Integer valueString value

Compares only values

Strict comparison

Page 14: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

14

Page 15: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

2. Conditional Statements

• if statement:

if(condition)

{

}

• if-else statement:if(condition)

{ }

else

{ }

Mubashir Ali - Lecturer (Department of Computer Science).

15

Page 16: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

2. Conditional Statements…

• switch statement:switch(variable)

{

case option:

action

break;

.

.

}

Mubashir Ali - Lecturer (Department of Computer Science).

16

Page 17: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

2. Conditional Statements…

Mubashir Ali - Lecturer (Department of Computer Science).

17

Switch starts

case 0

Case 1

Page 18: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

2. Conditional Statements…

Mubashir Ali - Lecturer (Department of Computer Science).

18

Page 19: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

3. Looping Statements

• for loop

• while loop

• do-while loop

• foreach loop

Mubashir Ali - Lecturer (Department of Computer Science).

19

Page 20: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

3. Looping Statements

• for loopfor($a=0; $a<10; $a++)

{

statements

}

• while loopwhile(condition)

{

Statements

Increment/decrement

}

Mubashir Ali - Lecturer (Department of Computer Science).

20

Page 21: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

3. Looping Statements…

• do-while loop

do

{

Statements

Increment/decrement

}

While(condition)

• foreach loop

– is used to read an entire array

Mubashir Ali - Lecturer (Department of Computer Science).

21

Page 22: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

3. Looping Statements…

Mubashir Ali - Lecturer (Department of Computer Science).

22

For loop

While loop

Page 23: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

3. Looping Statements…

Mubashir Ali - Lecturer (Department of Computer Science).

23

Output from for loop

Output from while loop

Page 24: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

3. Looping Statements…

Mubashir Ali - Lecturer (Department of Computer Science).

24

Array is declared

Foreach loop starts

Using obtained value

Page 25: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

3. Looping Statements…

Mubashir Ali - Lecturer (Department of Computer Science).

25

Page 26: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

4. Arrays in PHP

• An array is traditionally defined as a group of items that share certain characteristics

• Each item consists of two components:

– the key and a value

• PHP doesn’t require that you assign a size to an array at creation time

• Declaring an array:

– $array-name[key]=value

Mubashir Ali - Lecturer (Department of Computer Science).

26

Page 27: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

4. Arrays in PHP…

• Declaring an array:

– $array-name[key]=value

– $players[0]=“Muhammad Yousuf”;

• Adding element in an array:

– $players[1]=“Younus Khan”;

• Accessing element in an array

– echo $players[0];

Mubashir Ali - Lecturer (Department of Computer Science).

27

Page 28: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

4. Arrays in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

28

Declaring array

Adding elements

Foreach loop

Page 29: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

4. Arrays in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

29

Page 30: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

4. Arrays in PHP…

• Associative arrays:

– $array-name[‘element-name’]=value

– $players[‘yousuf’]=“Muhammad Yousuf”;

• Adding element in an array:

– $players[‘younus’]=“Younus Khan”;

• Accessing element in an array:

– echo $players[‘yousuf’];

Mubashir Ali - Lecturer (Department of Computer Science).

30

Page 31: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

4. Arrays in PHP…

• The array(); can also be used to create an array

– $array_name=array(item_1,item_2,…item_n);

– $players=array(“M.Yoursuf”,”Imran Khan”);

– $players=array(“Yousuf”=>“M.Yoursuf”,”imran”=>”Imran Khan”);

Mubashir Ali - Lecturer (Department of Computer Science).

31

Page 32: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

4. Arrays in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

32

Associative array is created using array()

Accessing elements by name

Page 33: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

4. Arrays in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

33

Page 34: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

4. Arrays in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

34

• Sorting arrays:

– sort()

– rsort()

Page 35: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

4. Arrays in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

35

Array is created

Array is sorted

Array is displayed

Page 36: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

4. Arrays in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

36

Page 37: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

4. Arrays in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

37

rsort() is used

Page 38: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

4. Arrays in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

38

Page 39: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

Summary

• Operators in PHP

• Conditional statements

• Looping statements

• Arrays in PHP

Mubashir Ali - Lecturer (Department of Computer Science).

39

Page 40: Lecture 20 Introduction to PHP (Part-2)€¦ · Introduction to PHP (Part-2) Mr. Mubashir Ali Lecturer (Dept. of Computer Science) dr.mubashirali1@gmail.com 1 Lecture 20

References

• Chapter 2, “Beginning PHP6,Apache,Mysql web development” by Matt Doyle, Wroxpublishers, 2009, ISBN: 0470413964

• Chapter 5, “Beginning PHP and MySQL” by W. Jason Gilmore, Apress publisher, 4th edition; 2010, ISBN-13 (electronic): 978-1-4302-3115-8.

Mubashir Ali - Lecturer (Department of Computer Science).

40


Recommended