Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS428 Web Engineering Lecture 20 Control Structures, Loops and Pointers File Uploading Function (PHP - III)

Similar presentations


Presentation on theme: "1 CS428 Web Engineering Lecture 20 Control Structures, Loops and Pointers File Uploading Function (PHP - III)"— Presentation transcript:

1 1 CS428 Web Engineering Lecture 20 Control Structures, Loops and Pointers File Uploading Function (PHP - III)

2 Format: if (expression) statement; Example: if ($a > $b) echo “a is larger than b”; Logical Expression: If Statement

3 or (if you have more than one line of code) if ($a > $b){ echo “a is larger than b”; }

4 EXAMPLE <?php $a = 4; $b = 3; if ($a > $b){ echo “a is larger than b”; } ?> Boolean Operators: >, >=, <, <=, ==, !=

5 EXAMPLE <?php $a = 3; $b = 4; if ($a > $b) { echo “a is larger than b”; } if ($a < $b){ echo “a is not larger than b”; } ?>

6 <?php $a = 3; $b = 4; if ($a > $b) { echo “a is larger than b”; } else { echo “a is not larger than b”; } ?>

7 EXAMPLE How to deal with different cases: <?php $a = 4; $b = 4; if ($a > $b) { echo “a is larger than b”; } elseif ($a == $b) { echo “a equals to b”; } else { echo “a is smaller than b”; } ?>

8 LOGICAL OPERATORS (AND) <?php $a = 5; $b = 4; ?> <?php $c = 20; $d = 1; if (($a > $b) && ($c > $d)){ echo “a is larger than b AND ”; echo “c is larger than d ”; } ?>

9 LOGICAL OPERATORS (OR) <?php $a = 5; $b = 4; ?> <?php $c = 1; $d = 20; if (($a > $b) || ($c > $d)){ echo “a is larger than b OR ”; echo “c is larger than d ”; } ?>

10 EXAMPLE If variable a is not set, then set equals to 100. <?php $a = 5; $b = 4; ?> <?php if (!isset($a)){ $a = 100; } echo $a; ?>

11 EXAMPLE If variable a is not set, then set equals to 100. <?php $a = 5; $b = 4; ?> <?php unset($a); if (!isset($a)){ $a = 100; } echo $a; ?>

12 If a is integer, then set its type equals to string. <?php $a = 5; $b = 4; ?> <?php if (is_int($a)) { settype($a, “string”); } echo gettype($a); ?>

13 Logical Expressions: Switch <?php switch ($a){ case 0: echo “a equals 0”; break; case 1: echo “a equals 1”; break; case 2: echo “a equals 2”; break; default: echo “a is not 0, 1 or 2”; break; } ?>

14 LOOPS Loops allow us to execute the code until the condition is satisfied. There are three main kinds of loops: while Loops for Loops foreach Loops

15 WHILE LOOPS The format of while loops is as under: while (expression) statement; The expression would be Boolean statement,

16 EXAMPLE <?php $count = 0; while ($count <= 10) { echo $count. “, ”; $count++; } echo “ Count: {$count}”; ?>

17 EXAMPLE <?php $count = 0; while ($count <= 10) { if ($count == 5) { echo “FIVE”; } else { echo $count. “, ”; } $count++; } echo “ Count: {$count}”; ?>

18 FOR LOOP The format of for loops is as under: for (expr1, expr2, expr3) statement; The expression would be boolean statement, Expression1 could be initializing statement. Expression2 could be condition/test. Expression3 could be increment/decrement.

19 EXAMPLE <?php for ($count = 0; $count <= 10; $count++ ) { echo $count. “, ”; } ?>

20 $_POST We use post method in a form. When we submit the button of form, all the data of form is send to $_POST array. We get this data on any other page from this super global array $_POST.

21 EXAMPLE Myform.php dataform.php <?php If($_POST) { $Name = $_POST[‘txtname’]; $Name =$_REQUEST[‘txtname’]; echo “Hello”. $Name; } ?>

22 $_GET T he data that we send through URL also known as query string is stored in this super global array $_GET. http://www.example.com/search.php?keyword=pakistan Query string started in URL after ? Sign. In a URL there may be more than one query string. http://www.example.com/search.php?keyword=paki stan&city=isb

23 EXAMPLE link.php Sending QueryString get.php <?php $siteName = $_GET[‘site’]; echo “Welcome to ”. $siteName; ?>

24 $_FILES When we upload a file or an image to server, it stores in $_FILES array. Example: User uploads a file from his computer c:\docs\project.zip Its size is 20,000 bytes PHP store in a temp folder, and gives it a random name. $_FILES[userfile][name] $_FILES[userfile][size] $_FILES[userfile][type]

25 EXAMPLE Upload_form.php Select image to upload:

26 Upload.php <?php move_uploaded_file($_FILES[' fileToUpload ']['tmp_name'], 'uploads/'.$new_file_name); $message = ‘Your file was accepted.'; ?>

27 FOREACH LOOP The format of foreach loops is as under: foreach ($array as $value) statement; The boolean test foreach loop is, do we have items still left in array. Foreach loop will go through every single element of an array. Foreach loop only works with array. Foreach loop works, it stores elements in array in a temporary variable

28 EXAMPLE <?php $agesArray = array(4, 8, 15, 17, 23, 42); ?> <?php // using each value foreach ($agesArray as $age) { echo $age. “, ”; } ?>

29 Syntax for associative array Foreach loop can take each array as a key value pair. foreach ($array as $key => $value) statement;

30 EXAMPLE <?php $ages = array(4, 8, 15, 16, 23, 42); ?> <?php // using each key => value pair // key for 4 is 0, key for 8 is 1 and so on… foreach ($ages as $position => $age) { echo $position. “: ”. $age. “ ”; } ?>

31 <?php $prices = array(“Brand new computer”=>2000, “1 month in lynda.com training center”=>25, “Learning PHP”=> “priceless”); foreach ($prices as $key => $value) { if (is_int($value)) { echo $key. “: $”. $value. “ ”; } else { echo $key. “: ”. $value. “ ”; } ?>

32 LOOPS: Continue If we didn’t want to print let say 5 <?php for ($count=0; $count <=10; $count++) { if ($count == 5) { continue; } echo $count. “, ”; } ?> Output: 1, 2, 3, 4, 6, 7, 8, 9, 10,

33 Lets say we are looking through, a list of students in a database. We can perform lots of complex action on it. But the very first thing we check, whether or not the student is fresh man, s/w engineer jr. or senior. If we turns out, the student is fresh man, we don’t want anything else. Then we can have this check, are they fresh man, continue

34 LOOPS: Break If we didn’t want to print before 5 <?php for ($count=0; $count <=10; $count++) { if ($count == 5) { break; } echo $count. “, ”; } ?> Output: 1, 2, 3, 4,

35 If we don’t want comma (,) at the end of last number. <?php for ($count = 0; $count <= 10; $count++) { echo $count; if ($count == 10) { break; } echo “, ”; } ?> Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

36 POINTERS Computer maintains the pointer, that’s points to the current value of an array. By default its always the first value. When we looping through array, computer moves that pointer along the array to get each value.

37 EXAMPLE <?php $ages = array(4, 8, 15, 16, 23, 42); ?> <?php echo “1: ”. current($ages). “ ”; // that will give us, the current place, the pointer is pointing. ?> Output: 1: 4

38 <?php $ages = array(4, 8, 15, 16, 23, 42); ?> <?php echo “1: ”. current($ages). “ ”; next($ages); echo “2: ”. current($ages). “ ”; reset($ages); echo “3: ”. current($ages). “ ”; ?> Output: 1: 4 2: 8 3: 15

39 EXAMPLE <?php $ages = array(4, 8, 15, 16, 23, 42); ?> <?php // while loop that moves the array pointer while ($age = current($ages)) { echo $age. “, ”; next($ages); } ?> Output: 4, 8, 15, 16, 23, 42,


Download ppt "1 CS428 Web Engineering Lecture 20 Control Structures, Loops and Pointers File Uploading Function (PHP - III)"

Similar presentations


Ads by Google