Page 1 of 1

Conditional Structures

Posted: Fri Jan 07, 2005 8:54 am
by ner0
Conditional Statements

if/then/else

probably the most common of the conditional statements, throughout most major programming and scripting languages aswell as PHP, is the if/then/else statement.
the basic syntax for the if/then/else statement is:
[quote][/quote]

however, if you prefer to use a structure including braces { and }, the following syntax would be the most appropriate:
[quote][/quote]
script can be seen in action [url="http://ner0.killagraphix.com/php/evenorodd.php"]here[/url]

notice the '%' comparison operator, this is technically referred to as the modulus, it basically remainder of object1 devided by object2, in our case determining whether an object is even/odd...

more on comparison operators can be found at [url="http://www.php.net/manual/en/language.operators.php"]php.net/manual/en[/url]
and will probably be covered in a later how-to

Conditional Structures

Posted: Sat May 28, 2005 1:10 pm
by Scott
Short if-then-else

A if-then-else statement can also be written using a ternary operator. This provides a simpler statement which can be included in a complex operation. For example, the following code:

Code: Select all

<?php

$num = 4;

if ($num > 0) 
{
     echo "Positive number";
}
else 
{
    echo "Negative number";
}

?>
could be written as:

Code: Select all

<?php

$num = 4;

echo ($num > 0 ? "Positive number" : "Negative number");

?>

The switch (case) Statement

The case statement can be used to replace a complex if-then-else statement which can often cause code to get cluttered. The case statement compares an expression to all possible case expressions listed. The match is made using the regular equality operator (==) and does not use the identical operator (===). You can use the break statement to end execution and skip to the code following the switch construct. The break statement is essential, otherwise the script will continue to execute through the expressions until it reaches the end of the switch construct.

If no match is found, and the default statement is included in the switch construct then it will be executed. If the default statement is included it must be appear at the end of the cases or not appear at all. The general switch is formed like so:

Code: Select all

switch (expr) {
        case expr:
               statement;
        case expr:
               statement;
        ...
        default:
               statement;
}
Here is an example of a complete switch statement. This will output whether the $answer variable stores either "Y" or "y" (in which case it outputs "yes") or if it stores "N" or "n" (which outputs "no"). If neither condition is met, an error is returned.

Code: Select all

switch ($answer) {
	case 'Y':
	case 'y':
  echo "The answer is yes";
  break;
	case 'N':
	case 'n':
  echo "The answer is no";
  break;
	default:
  echo "An error occured, the answer is neither yes or no";
  break;
}
This could also have been achieved using multiple if-then-else statments like so:

Code: Select all

if ($answer == "Y" or $answer == "y")
{
	echo "The answer is yes";
}
elseif ($answer == "N" or $answer == "n") 
{
	echo "The answer is no";
}
else
{
	echo "An error occured, the answer is neither yes or no";
}

Hope this helps explain alternatives to the standard if-then-else which is shown in the post above /smile.gif' class='bbc_emoticon' alt=':)' /> Feel free to ask questions if this hasn't explained fully.