Operators

1. Comparison Operators



== Has the same value e.g $variable == $variable2


!=  Does NOT have the same value  e.g $variable != $variable2


>   Less than e.g $variable > $variable2


<   Greater than e.g $variable < $variable2


>= Greater than or equals to e.g $variable >= $variable2


<=  Less than or equals to e.g $variable <= $variable2



Conditional Logic 


1. If Statements    


You learn that variables are used to store some information that you can use later whenever required,you can save text and numbers. You store information so that you can do something with them. If you have stored a username in a variable and need to check if this is a correct username. For this you need to use "IF" statement.


if(condition)


{


// Start with { and end with } and any thing written between these two {} are execute under if condition


}



$username = "Nemo";


$number = "19051981";



if( $username == "Nemo" )


{


    echo "Correct username";


}


if( $username != "Doree" )


{


    echo "If condition OK as our variable has Nemo saved in it and we are comparing Nemo with Doree that are two different strings so we are in if statement.";


}


if( $number == "2031" ) // Did not go into this statement


{


    echo " ";


}



if( $number == "19051981" )


{


    echo "Correct number";


}


$number2 = "19051981";


if( $number == $number2 )


{


    echo "Correct number";


}



" == (Double equal sign)" used to compare two strings or numbers.
" != (Sign of Exclamation and equal sign)" used to compare if two strings or numbers are not equal to each other


2. if ... else


IF ELSE statement used when your IF statement doesnot comes true than you use the ELSE part. For example if the values of two variables are same you want to print out TRUE (i.e variables have same values) and if the values of the two variabales are not equal (i.e variables have different values) than you want to print FALSE,here ELSE part of IF used.


<?


$username = "Nemo";


$number = "19051981";



if( $username == "Nemo" )


{


    echo "TRUE";


}


else


{


    echo "FALSE";


}



if( $username != "Doree" )


{


    echo "TRUE";


}


else


{


    echo "FALSE";


}


?>


<?


$usaFlag = 0;
$ukFlag = 1;


if ( $usaFlag == 0 )


{
    echo "<IMG SRC=images/usaFlag.jpg>";
}
else


{
    echo "<IMG SRC=images/ukFlag.jpg>";
}


?>



3. if ... else if


<?


$usaFlag = 0;
$ukFlag = 1;


if ( $usaFlag == 1 )


{
    echo "<IMG SRC=images/usaFlag.jpg>";
}
else if ( $ukFlag == 1 )


{
    echo "<IMG SRC=images/ukFlag.jpg>";
}


else


{


    echo "No value equal to 1 found.";


}


?>


 


 

                    

Copyright © 2008 VisualBuilder. All rights reserved