Skip to main content

PHP Control Flow (if...elseif....else)


       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
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

Popular posts from this blog

PHP INTRODUCTION

                     PHP  (recursive acronym for  PHP: Hypertext Preprocessor ) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. PHP stands for  P HP:  H ypertext  P reprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP is an open source software PHP is free to download and use Why PHP? PHP runs on different platforms (Windows, Linux, Unix, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP is FREE to download from the official PHP resource:  www.php.net PHP is easy to learn and runs efficiently on the server side What can PHP do? Anything. PHP is mainly focused on server-side scripting, so you can do anything any other CGI program can do, such as collect form data, generate dynam

MySQL General Architecture

        MySQL operates in a networked environment using a client/server architecture. In other words, a central  program acts as a server, and various client programs connect to the server to make requests. A MySQL  installation has the following major components: MySQL Server, Client programs and MySQL non client  utilities.  MySQL Server MySQL Server, or mysqld, is the database server program. The server manages access to the actual  database (schema) on disk and in memory. MySQL Server is multi-threaded and supports many  simultaneous client connections. Clients can connect via several connection protocols. For managing  database contents, the MySQL server features a modular architecture that supports multiple storage engines  that handle different types of tables (for example, it supports both transactional and non-transactional  tables). Keep in mind the difference between a server and a host. The server is software (the MySQL server  program mysqld). Server characteristi