"> ">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.

Similar presentations


Presentation on theme: "Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens."— Presentation transcript:

1 Unit – 3 Control structures

2 Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens if you do not work hard? Well, you fail! This is an example of an if/else conditional statement.

3 <?php $number_three = 3; if ( $number_three == 3 ) { echo "The if statement evaluated to true"; } else { echo "The if statement evaluated to false"; } ?>

4 <?php $employee = “xyz”; if($employee == “abc") { echo "Hello Ma'am"; } elseif($employee == “xyz") { echo "Good Morning Sir!"; } else { echo "Morning"; } ?>

5 2. Switch Use the switch statement to select one of many blocks of code to be executed. switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; default: code to be executed if n is different from both label1 and label2; }

6 <?php $x=1; switch ($x) { case 1: echo "Number 1"; break; case 2: echo "Number 2"; break; case 3: echo "Number 3"; break; default: echo "No number between 1 and 3"; } ?>

7 3. The Ternary Operator It is called the ternary operator because it takes three operands - a condition, a result for true, and a result for false.

8 Loops 1.while:- The meaning of a while statement is simple. It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE. Syn:- while (expr): statement... endwhile;

9 <?php $i = 1; while ($i <= 10) { echo $i++; /* the printed value would be $i before the increment (post-increment) */ }

10 2. Break:- break ends execution of the current for, for each, while, do-while or switch structure. break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.

11 <?php $i=0; while($i<11) { switch($i++) { case 5: echo "5th......&nbsp "; /* Exit only the switch. */ break 1; case 10: echo "10th....."; /* Exit the switch and the while. */ break 2; default: break; } ?>

12 3. Continue:- continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

13 <?php $x=0; echo 'List of odd numbers between 1 to 10 '; while ($x<10) { $x++; if ($x==3) { continue; } echo $x.' '; } ?>

14 4. Do…. While:- do-while loops are very similar to while loops, except the truth expression is checked at the end of each iteration instead of in the beginning. '; do { echo 'i value is '. $i. ', so code block will run. '; ++$i; } while ($i

15 5.For loop:- The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop. In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends. for (expr1; expr2; expr3) statement E.X:-

16 6. ForThis simply gives an easy way to iterate over arrays. foreach works only on arrays, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes; the second is a minor but useful extension of the first: foreach (array_expression as $value) statement OR foreach (array_expression as $key => $value) statement

17 <?php $arr = array(1, 2, 3, 4); foreach ($arr as &$value) { $value = $value * 2; } // $arr is now array(2, 4, 6, 8) unset($value); // break the reference with the last element ?>

18 <?php //$a=array(38,23); $a[1] = "32"; $a['Quagmire'] = "30"; $a['Joe'] = "34"; foreach($a as $key1=>$value1) echo "$key1:$value1 "; ?>

19

20 $value) echo "$key: $value\n"; var_dump(key($a)); /* Produces: 0: zero 1: one 2: two 3: three int(1) */ ?>

21 Exit() exit — Output a message and terminate the current script Description void exit ([string $status ] ) void exit ( int $status ) //exit program normally exit; exit(); exit(0); //exit with an error code exit(1);

22 <?php $x=0; echo 'List of odd numbers between 1 to 10 '; while ($x<10) { $x++; if ($x==3) { exit(); } echo $x.' '; }?>

23 Die() die — Equivalent to exit() Description:- This language construct is equivalent to exit().

24 Return() If called from within a function, the return() statement immediately ends execution of the current function, and returns its argument as the value of the function call.

25 <?php function square($num) { return $num * $num; } function a($num,$num1) { return $num+$num1; } echo square(4)." "; echo a(4,5) ?>

26 Arrays in PHP An array is a variable that holds a group of values. Arrays are usually meant to store a collection of related data elements, although this is not a necessity. You access the individual elements by referring to their index position within the array. The position is either specificied numerically or by name

27 $arrName=array( key => value,... ) // key may be an integer or string // value may be any value OR $arr[key] = value; $arr[] = value; // key may be an integer or string // value may be any value

28 <?php $a=array(‘xyz’=>38,2=>23); /*$a[1] = "32"; $a['Quagmire'] = "30"; $a['Joe'] = "34";*/ echo "Peter is ". $a[2]. " years old."; ?>

29 "bar", 12 => true); echo $arr["foo"]; // bar echo $arr[12]; // 1 ?>

30 1, 12 => 2); $arr[] = 56; // This is the same as $arr[13] = 56; // at this point of the script $arr["x"] = 42; // This adds a new element to // the array with key "x" unset($arr[5]); // This removes the element from the array unset($arr); // This deletes the whole array ?>


Download ppt "Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens."

Similar presentations


Ads by Google