Home PHP Ajax Regex Interviews Contact us    

PHP


Interview Questions



Conditional Statements
Conditional Statements allow you to branch the path of execution in a script based on whether a single, or multiple conditions, evaluate to true or false. Put simply, they let you test things and perform various actions based on the results.

If Statements

Take a look at the following:

<?php
$x=1;

if ($x == 1) print '$x is equal to 1';
?>


This example illustrates the simplest kind of If Statement. If Statements always begin with "if", followed by a condition surrounded in parentheses. If the condition evaluates to true, then the statement or statements immediately following the condition will be executed. In this case, had the condition been false, nothing would have occurred and you would have seen a blank browser window when the script was run.

When you have more than one statement to be executed within a control structure, it's necessary to surround them with brackets:

<?php
$x=1;

if ($x == 1) {
   print '$x is equal to 1';
   $x++;
  
print 'now $x is equal to 2';
}
?>



Keep in mind that the positioning of the elements does not affect the execution of the script. All of the example arrangements below are perfectly valid not only for If Statements, but for every form of control loop.


if ($x == 1) print '$x is equal to 1';

if ($x == 1)
   print '$x is equal to 1';

if ($x == 1) { print '$x is equal to 1'; }

if ($x == 1) {
   print '$x is equal to 1';
}


In the interests of clarity, many programmers opt to use indenting and brackets even on one-line blocks of code; however, it is ultimately a matter of personal coding preference.

You can also include multiple conditions within parentheses. For the nested statements to execute, all of the conditions must evaluate to true.

<?php
$x=1;

if ($x == 1 OR $x == 2) print '$x is equal to 1 (or maybe 2)';
?>


Else Statements

As the name implies, Else Statements allow you to do something else if the condition within an If Statement evaluated to false:

<?php
$x=1;

if ($x == 2) {
print '$x is equal to 2';
} else {
   print '$x is equal to 1';
}
?>


Else If Statements


Thus far, we have been able to respond to one condition, and do something if that condition is not true. But what about evaluating multiple conditions? You could use a series of If Statements to test each potential condition, but in some situations that is not a suitable option. Here is where Else If Statements come in.

A combination of If and Else Statements, Else If Statements are evaluated sequentially if the condition within the If Statement is false. When a condition within an Else If Statement evaluates to true, the nested statements are parsed, the script stops executing the entire If/Else if/Else Structure. The rest of the script proceeds to be parsed.

Take a look at the following example:

<?php
$x=1;

if ($x == 2) {
print '$x is equal to 2';
} else if ($x == 1) {
print '$x is equal to 1';
} else {
   print '$x does not equal 2 or 1';
}
?>


The final else statement can be left off if you do not want anything to happen if none of the If or Else If Statements are true:

<?php
$x=0;

if ($x == 2) {
print '$x is equal to 2';
} else if ($x == 1) {
print '$x is equal to 1';
}
?>


In this case, since neither the condition within the If or Else if Conditions are true, and no Else Statement was provided, nothing would be outputted to the browser.

Switches

Switches are a good alternative to If/Else if/Else Statements in situations where you want to check multiple values against a single variable or condition. This is the basic syntax:

<?php
$var = "yes";

switch ($var) {
   case "yes":
     print '$var is equal to yes';
break;

   case "no":
    
print '$var is equal to no';
     break;
}
?>


After running this code snippet, much of what is here will probably make sense to you. In the first line of the switch statement, we have the identifier "switch" followed by a variable surrounded by parenthesis. Each case includes a possible value for the variable.

Switches execute a little differently than If/Else if/Else statements. Once a case value matches the value of the switch expression, every following statement is executed, including those following other cases.

To prevent this from happening, a break statement is used. "Break;" ends the execution of the switch statement, and lets the script continue execution; it can also be used in while or for loops.

Optionally, you may also include a special case called "default". This case works much like and Else Statement, and will execute if all the other cases are found to be false. This case should be the very last one you include.

<?php
$var = "yes";

switch ($var) {
   case "maybe":
     print '$var is equal to yes';
break;

   case "no":
    
print '$var is equal to no';
     break;
 
   default:
     print 'none of the other two cases were true, so this sentance will be printed out instead.';
}
?>


Similar to the Break Statement is the Exit Statement. Exit is particularly useful in situations where you run into what would be considered a "fatal error" (for example, if the user had entered a password that was incorrect) or any other time you needed to end the execution of a script before it naturally terminated.

<?php
$var = "yes";

switch ($var) {
   case "yes":
     print '$var is equal to yes';
     exit;
   case "no":
     print '$var is equal to no';
     break;
}

print "this will not be printed, because the script will have terminate before this line is reached";
?>


Unlike break, exit may be used anywhere in your scripts, inside or outside of control structures.


The Ternary Operator

Though Technically an Operator, not a Control Structure, the Ternary Operator, represented by "?", can be used as shorthand for simple If/Else Statements. It can only be used in situations where you want execute a single expression based on whether a single condition is true or false:

<?php
$x = 1;
($x==1) ? (print '$x is equal to 1') : (print '$x is not equal to 1');
?>


The condition is contained within the first set of parentheses. If it evaluates to true, then the expression within the second set of parentheses will be performed. Otherwise, the expression in the third set will be performed:

(condition) ? (executes if the condition is true) : (executes if the condition is false);


Feed Back of this Topic
 
Name :
Email :
Topic :
Comments :

Ajax


Regular Expression


 
Copyright © 2007 123developers.com
Contact us | Disclaimer