Conditional Structures

Support, Snippets & Projects

Moderators: Moderator, Global Moderator

Post Reply
ner0
Hero Member
Hero Member
Posts: 982
Joined: Sat May 22, 2004 4:45 pm

Conditional Structures

Post by ner0 »

[b][u]Conditional Statements[/u][/b]

[b]if/then/else[/b]

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]    if ([i]expression[/i])
        statement
?>[/quote]

however, if you prefer to use a structure including braces { and }, the following syntax would be the most appropriate:
[quote]    if ([i]expression[/i])
    {
        statement
    }[/quote]

values for [i]expression[/i] can be any of a multitude of things... an example which is often used is:
[quote]    if (\"string1\" == \"string2\")
    {
        statement
    }[/quote]
this \"==\" operator first forces the two strings to have the same data type, which is in this case fine, because both values are strings...

if both of these values are identical, the action within the braces will be executed, however it is obvious that these two values are not equal, so at this point it will probably become necessary to add an alternative, so if the values are not equal, a seperate defined action will occur.
for this scenario the following syntax would be required:
[quote]    if (\"string1\" == \"string2\")
    {
          statement
    }
    else
    {
          statement2
      }[/quote]

(please note that almost all actions you will want to execute between the braces (shown in these examples by \"statementX\"), will require a subsequent semi-colon to avoid fatal parsing errors.)

here\'s an example script to give you an idea of a practical situation.

[quote]// echo the even/odd \'ness of all numbers below $y.
$y = \'10\';

for ($x = \'0\'; $x < $y; $x++) {
  // check to see if the number is odd/even
  if (($x % \'2\') == \"0\") // if the remainder of a devision by 2 is 0, it\'s even, otherwise it\'s odd.
  {
        echo $x . \" is [b]even[/b]\";
    }
    else
    {
        echo $x . \" is [b]odd[/b]\";
    }
}
?>[/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
Last edited by ner0 on Fri Jan 07, 2005 5:30 pm, edited 1 time in total.
[color=\"green\"]'class KPIM::ProcessManager' only defines private constructors and has no friends[/color] <- :( poor class



[18:09:00] * ~Moppy stews ner0 erotically 1 times.
Scott
Administrator
Administrator
Posts: 1651
Joined: Sun Apr 25, 2004 1:08 pm

Conditional Structures

Post by Scott »

[b]Short if-then-else[/b]

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

$num = 4;

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

?>[/code]

could be written as: [code]<?php

$num = 4;

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

?>[/code]


[b]The switch (case) Statement[/b]

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

If no match is found, and the [i]default[/i] statement is included in the [i]switch[/i] construct then it will be executed. If the [i]default[/i] statement is included it must be appear at the end of the cases or not appear at all. The general [i]switch[/i] is formed like so:[code]switch (expr) {
       case expr:
              statement;
       case expr:
              statement;
       ...
       default:
              statement;
}[/code]
Here is an example of a complete [i]switch[/i] 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]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;
}[/code]
This could also have been achieved using multiple if-then-else statments like so:[code]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";
}[/code]


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.
Post Reply

Return to “PHP &amp; MySQL”