Conditional Structures
Posted: Fri Jan 07, 2005 4:54 pm
[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
[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