USING CONDITIONAL CODE AMIR KHANZADA
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 Else Else if switch
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 $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 ){ …
&& LOGICAL AND / OR 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 $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.