Conditional Code (Day-1) Looping & Condition

ssuser2b99481 7 views 19 slides Oct 09, 2024
Slide 1
Slide 1 of 19
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19

About This Presentation

Conditional Code - Looping


Slide Content

Looping & Branching

PHP scripts are a series of instructions in a file. PHP begins at the top of the file and executes each instruction, in order, as it comes to it. However, some scripts need to be more complicated. You may want your script to display one page to new customers and a different page to existing customers . We need our code to be smarter, we need to start asking questions, if the bank balance is positive calculate interest if negative charge a penalty. Or you may need to display a list of phone numbers by executing a single echo statement repeatedly, once for each phone number. Controlling the Flow of the Script

PHP provides two types of complex statements that enable you to perform tasks like this — tasks that change the order in which statements are executed: Conditional statements Looping statements Complex Statement

Conditions are expressions that PHP tests or evaluates to see whether they are true or false. Conditions are used in complex statements to determine whether or not a block of simple statements should be executed. To set up conditions, you compare values. Setting Up Conditions

Are two values equal? Is one value larger or smaller than another? Does a string match a pattern? Setting Up Conditions

Conditional Statement Conditional statements are the set of commands used to perform different actions based on different conditions .   In PHP we have the following conditional statements: If E lse Else if S witch

If Statement If structure is used for conditional execution of code segment. If something is true, then do something. Syntax : if (expr) { Statements  }   

If Statement <?php if ($c > $d)  {  echo "c is bigger than d";  } ?>  In the above example, only if the condition "$c>$d" is true, the message "c is bigger than d" is displayed

CONDITIONAL CODE If ( ) { } condition // your additional code goes here // . . .

TERMINOLOGY parentheses brackets braces ( ) { } [ ]

CONDITIONAL CODE If ( ) { } condition echo “it’s true!”; // . . . $a < 50 $b > 20 true or false? $c == 99 $c === 99 $d != 100 Code block

EQUALITY If ( $a == $b ){ // execute this code }

ASSIGNMENT INSTEAD OF EQUALITY $a = 5; $b = 10; If( $a = $b ){ // always true! }

OPERATOR WITH = = assignment == equality === strict equality

COMPARISON If( $a == $b ){ … If( $a != $b ){ … If( $a === $b ){ … If( $a !== $b ){ … If( $a > $b ){ … If( $a < $b ){ … If( $a >= $b ){ … If( $a <= $b ){ …

Using comparison operators

&& LOGICAL AND / OR If( $a === $b $c === $d){… If( $a === $b $c === $d){… If( ($a > $b) && ($c > $d) ){… and or ||

Else Statement If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement. Syntax if (condition) code to be executed if condition is true; else code to be executed if condition is false;

Else Statement <?php $c = 10;  $d = 20; if ($c > $d)  { echo "C is bigger than d"; }  else { echo "D is bigger than c"; }  ?> Result : D is bigger than C  In the above example in the if the condition "$c>$d" is true then the message "C is bigger than D" is displayed, else the message "D is bigger than C" is displayed.
Tags