SEO: Download Free software, Free Engineering Notes, The Infinite Info

What's New Here?

0

PHP : Conditional Logic


Conditional Logic is all about asking "What happens If ... ". When you press a button labelled "Don't Press this Button - Under any circumstance!" you are using Conditional Logic. You are asking, "Well, what happens If I do press the button?"

Conditional Logic uses the "If" word a lot. For the most part, you use Conditional Logic to test what is inside of a variable. You can then makes decisions based on what is inside of the variable.
In PHP, you use the "If" word like this:
if($username == "authentic") {
       //Code to let user access the site here;
}
The different conditional logics are:
  • if statement
  • else statement
  • else-if statement
  • switch statement
  • ternary statement
If statement
The if statement tests if a certain condition is true or false and acts upon it accordingly.
Syntax:
if(condition) { 
      perform this action;
}
If the condition in the parenthesis is true, then the code following the condition is executed, otherwise it will not.
Example:<?php 
    $number = 10;
    if ($number == 10) {
         echo "Number is equal to 10";
    }
?>
else statement
If the condition in the parenthesis in an if statement is true, then the code following the condition will be executed, otherwise the else statement is executed. The else statement works in conjunction with the if statement and executes certain code if the condition in the if statement is false.
Syntax:if (condition) { 
    perform action 
} else {
    perform action if above condition is false
}
Example:<?php
    $number = 5;
    if($number == 10) {
        echo "Number is equal to 10";
    } else {
        echo "Number is not equal to 10";
    }t
?
else-if statement
The if statement tests a single condition and performs an action if the condition is true and the else statement performs and action if the condition in the if statement is false, but if there are more than two possibilities then else-if statement is executed. The else-if statement is used in conjunction with if statement. Unlike the else statement, it does not specifically perform a certain action if the condition in the if statement  is false, but rather it performs an action if the condition in the if statement is another specific value specified in the else if statement itself.
Syntax:
if(condition){
    perform this action
} else if(another condition){
    perform this action
}
Example:
<?php 
    $number = 5;
    if($number == 10) {
        echo "Number is equal to 10";
    } else if ($number == 5) {
        echo "Number is equal to 5";
    }
?>
Switch statement
The switch statement is  specifically designed for comparing one variable to a number of possible values. It can be used as a substitute of if, else-if, else  structure. There is an important keyword break keyword. The break keyword is used to make sure that the switch statement will not fall through to the next possible value, even if the value is incorrect within the switch statement.
Syntax:
switch(variable){
    case possible value:
        perform this action;
        break;
    case possible value:
        perform this action;
        break;
    case possible value:
        perform this action;
        break;
    default:
        perform this action if none of the value matches;
}
Example:<?php 
    $number = 2;
    switch($number) {
        case 1:
            echo "Number is equal to 1";
            break;
        case 2:
            echo "Number is equal to 2";
            break;
        case 3:
            echo "Number is equal to 3";
            break;
        default:
            echo "Number does not matches the value";
    }
?>
The ternary operator re
The ternary operator is the question mark symbol(?) it works the same way as the if-else structure.
Syntax:
variable = (condition)?value the variable will take if condition is true: value of variable will take if condition is false
Example:<?php 
    $x;
    $y = 5;
    $x = ($y == 3)?6:12;
    echo "x=".$x;
?>
Filed in: , , ,

Get Updates

Share This Post

No comments:

Leave a Reply

2012 LEARN PHP BASICS. All Rights Reserved.
Designed by Theme Junkie, Converted by Uong Jowo