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

Post on 06-Jun-2020

7 views 0 download

transcript

Introduction to PHP (Part-2)

Mr. Mubashir AliLecturer (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

Outline

• Operators in PHP

• Conditional Statements in PHP

• Looping Statements

• Arrays in PHP

Mubashir Ali - Lecturer (Department of Computer Science).

3

1. Operators in PHP

Mubashir Ali - Lecturer (Department of Computer Science).

4

• Arithmetic Operators:

– +, - ,*, /, %

• Assignment Operators:

– =

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

– .= ($a .= $b)

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

5

Adds $b in $a

Concatenates $b with $a

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

6

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

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

8

First variable

Second variable

Concatenation

Using .=

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

9

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

10

Variable declared

Incremented before display

Incremented after display

Displaying incremented value

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

11

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

12

• Logical Operators:

– AND, OR, NOT, XOR

– &&, ||, !

• Equality Operators:

– ==, !=, ===

• Comparison Operators:

– >, <, <=, >=

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

13

Integer valueString value

Compares only values

Strict comparison

1. Operators in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

14

2. Conditional Statements

• if statement:

if(condition)

{

}

• if-else statement:if(condition)

{ }

else

{ }

Mubashir Ali - Lecturer (Department of Computer Science).

15

2. Conditional Statements…

• switch statement:switch(variable)

{

case option:

action

break;

.

.

}

Mubashir Ali - Lecturer (Department of Computer Science).

16

2. Conditional Statements…

Mubashir Ali - Lecturer (Department of Computer Science).

17

Switch starts

case 0

Case 1

2. Conditional Statements…

Mubashir Ali - Lecturer (Department of Computer Science).

18

3. Looping Statements

• for loop

• while loop

• do-while loop

• foreach loop

Mubashir Ali - Lecturer (Department of Computer Science).

19

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

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

3. Looping Statements…

Mubashir Ali - Lecturer (Department of Computer Science).

22

For loop

While loop

3. Looping Statements…

Mubashir Ali - Lecturer (Department of Computer Science).

23

Output from for loop

Output from while loop

3. Looping Statements…

Mubashir Ali - Lecturer (Department of Computer Science).

24

Array is declared

Foreach loop starts

Using obtained value

3. Looping Statements…

Mubashir Ali - Lecturer (Department of Computer Science).

25

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

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

4. Arrays in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

28

Declaring array

Adding elements

Foreach loop

4. Arrays in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

29

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

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

4. Arrays in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

32

Associative array is created using array()

Accessing elements by name

4. Arrays in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

33

4. Arrays in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

34

• Sorting arrays:

– sort()

– rsort()

4. Arrays in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

35

Array is created

Array is sorted

Array is displayed

4. Arrays in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

36

4. Arrays in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

37

rsort() is used

4. Arrays in PHP…

Mubashir Ali - Lecturer (Department of Computer Science).

38

Summary

• Operators in PHP

• Conditional statements

• Looping statements

• Arrays in PHP

Mubashir Ali - Lecturer (Department of Computer Science).

39

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