PHP starts with many strengths in its logical and bitwise operators. PHP then extends the ideas of C, Java and Perl control of flow and looping functionality - giving them more of the ease of use associated with Visual Basic or Ruby. Also enhancing the functionality of its associative array, PHP has some powerful control commands with list(), each() and foreach().
Finally, PHP takes full advantage of its mixed type capability for variables and functions. Thus functions like fopen() return a value (a file handle in the case of fopen() finding a file for opening) or the Boolean false if no file was found. Developers have to be alert to these different
Very often when you write code, you want to perform different actions for different decisions.
You can use conditional statements in your code to do this.
In PHP we have the following conditional statements:
- if statement - use this statement to execute some code only if a specified condition is true
- if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false
- if...elseif....else statement - use this statement to select one of several blocks of code to be executed
- switch statement - use this statement to select one of many blocks of code to be executed
Conditional Operators
PHP allows you to perform a number of different comparisons, to check for the equality or relative size of two values. PHP’s conditional operators are shown in Table 3.1.
Table 3.1 Conditional Operators in PHP
Operator
|
Description
|
==
|
Is equal to
|
===
|
Is identical to (is equal and is the same data type)
|
!=
|
Is not equal to
|
!==
|
Is not identical to
|
<
|
Is less than
|
<=
|
Is less than or equal to
|
>
|
Is greater than
|
>=
|
Is greater than or equal to
|
Logical Operators
You can combine multiple expressions to check two or more criteria in a single conditional statement. For example, the following statement checks whether the value of $number is between 5 and 10:
$number = 8; if ($number >= 5 and $number <= 10) { echo "$number is between five and ten"; }
The keyword and is a logical operator, which signifies that the overall condition will be true only if the expressions on either side are true. That is, $number has to be both greater than or equal to 5 and less than or equal to 10.
Table 3.2 shows the logical operators that can be used in PHP.
Table 3.2 Logical Operators in PHP
Operator
|
Name
|
Description
|
! a
|
NOT
|
True if a is not true
|
a && b
|
AND
|
True if both a and b are true
|
a || b
|
OR
|
True if either a or b is true
|
a and b
|
AND
|
True if both a and b are true
|
a xor b
|
XOR
|
True if a or b is true, but not both
|
a or b
|
OR
|
True if either a or b is true
|
You may have noticed that there are two different ways of performing a logical AND or OR in PHP. The difference between and and && (and between or and ||) is the precedence used to evaluate expressions.
Table 3.2 lists the highest-precedence operators first. The following conditions, which appear to do the same thing, are subtly but significantly different:
a or b and c a || b and c
In the former condition, the and takes precedence and is evaluated first. The overall condition is true if a is true or if both b and c are true.
In the latter condition, the || takes precedence, so c must be true, as must either a or b, to satisfy the condition.
if Conditions
Simple if statement
Syntax
if (conditions)
Do this;
In the above code, the Do this; statement will either run or not run depending on whether or not the conditions are true. This syntax can only be used when the condition affects a single line of code. For a block of code, use the following syntax.
Syntax
if (conditions)
{
Do this;
Then do this;
And this too;
}
The lines of code affected by the if condition are put in a code block, which is surrounded by curly brackets to indicate that all of the code either should or should not be executed, depending on the result of the if condition.
if-else statement
Syntax
if (conditions)
{
Do this;
}
else
{
Do that;
}
if-elseif-else statement
Syntax
if (conditions)
{
Do this;
}
elseif (other conditions)
{
Do that;
}
else
{
Do this other thing;
}
The two syntax blocks above show an if-else and an if-elseif-else statement, which can have any number ofelseif blocks.
EXAMPLE
<?php
$currentDate = time();
if (strtotime($_POST['DateEntered']) > $currentDate) {
echo 'That date is in the future';
} elseif (strtotime($_POST['DateEntered']) < $currentDate) {
echo 'That date is in the past';
} else {
echo 'That is today';
}
?>
<?php
$currentDate = time();
if (strtotime($_POST['DateEntered']) > $currentDate)
{
echo 'That date is in the future';
}
else if (strtotime($_POST['DateEntered']) < $currentDate)
{
echo 'That date is in the past';
}
else
{
echo 'That is today';
}
?>
Comments
Post a Comment