Setting the environment Overview of PHP Constants and Variables in PHP

40
Introduction to PHP (Part-2) Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakist an. 1

description

Setting the environment Overview of PHP Constants and Variables in PHP. Operators in PHP Conditional Statements in PHP Looping Statements Arrays in PHP. Arithmetic Operators: +, - ,*, /, % Assignment Operators: = += ($a +=$b ), *= , /= .= ($a .= $b). Adds $b in $a. - PowerPoint PPT Presentation

Transcript of Setting the environment Overview of PHP Constants and Variables in PHP

Page 1: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

1

Introduction to PHP (Part-2)

Page 2: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

2

Summary of the previous lecture

• Setting the environment• Overview of PHP• Constants and Variables in PHP

Page 3: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

3

Outline

• Operators in PHP• Conditional Statements in PHP• Looping Statements• Arrays in PHP

Page 4: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

4

1. Operators in PHP

• Arithmetic Operators:– +, - ,*, /, %

• Assignment Operators:– =–+= ($a +=$b ), *= , /=– .= ($a .= $b)

Page 5: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

5

1. Operators in PHP…

Adds $b in $a

Concatenates $b with $a

Page 6: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

6

1. Operators in PHP…

Page 7: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

7

1. Operators in PHP…

• String Operators:– . , .=– $a=“abcd”.”efgh”; $a=abcdefgh– $a.=“ijk”; $a=abcdefghijk

• Increment/decrement Operators:– ++,--–$b=$a++–$b=++$a

Page 8: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

8

1. Operators in PHP…

First variable

Second variable

Concatenation

Using .=

Page 9: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

9

1. Operators in PHP…

Page 10: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

10

1. Operators in PHP…

Variable declared

Incremented before display

Incremented after display

Displaying incremented value

Page 11: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

11

1. Operators in PHP…

Page 12: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

12

1. Operators in PHP…

• Logical Operators:– AND, OR, NOT, XOR– &&, ||, !

• Equality Operators:– ==, !=, ===

• Comparison Operators:– >, <, <=, >=

Page 13: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

13

1. Operators in PHP…

Integer valueString value

Compares only values

Strict comparison

Page 14: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

14

1. Operators in PHP…

Page 15: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

15

2. Conditional Statements

• if statement:if(condition) { }

• if-else statement:if(condition)

{ } else

{ }

Page 16: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

16

2. Conditional Statements…

• switch statement:switch(variable){case option: actionbreak;..}

Page 17: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

17

2. Conditional Statements…

Switch starts

case 0

Case 1

Page 18: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

18

2. Conditional Statements…

Page 19: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

19

3. Looping Statements

• for loop• while loop• do-while loop• foreach loop

Page 20: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

20

3. Looping Statements

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

• while loopwhile(condition){StatementsIncrement/decrement}

Page 21: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

21

3. Looping Statements…

• do-while loopdo{StatementsIncrement/decrement}While(condition)

• foreach loop– is used to read an entire array

Page 22: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

22

3. Looping Statements…

For loop

While loop

Page 23: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

23

3. Looping Statements…

Output from for loop

Output from while loop

Page 24: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

24

3. Looping Statements…

Array is declared

Foreach loop starts

Using obtained value

Page 25: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

25

3. Looping Statements…

Page 26: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

26

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

Page 27: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

27

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];

Page 28: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

28

4. Arrays in PHP…

Declaring array

Adding elements

Foreach loop

Page 29: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

29

4. Arrays in PHP…

Page 30: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

30

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’];

Page 31: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

31

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”);

Page 32: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

32

4. Arrays in PHP…

Associative array is created using array()

Accessing elements by name

Page 33: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

33

4. Arrays in PHP…

Page 34: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

34

4. Arrays in PHP…

• Sorting arrays:– sort()– rsort()

Page 35: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

35

4. Arrays in PHP…

Array is created

Array is sorted

Array is displayed

Page 36: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

36

4. Arrays in PHP…

Page 37: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

37

4. Arrays in PHP…

rsort() is used

Page 38: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

38

4. Arrays in PHP…

Page 39: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

39

Summary

• Operators in PHP• Conditional statements• Looping statements• Arrays in PHP

Page 40: Setting the environment Overview of PHP Constants and Variables in PHP

Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.

40

References

• Chapter 2, “Beginning PHP6,Apache,Mysql web development” by Matt Doyle, Wrox publishers, 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.