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 // . . .
&& 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.