Conditional Structures in PHP
PHP Control Structures with Security Considerations - Part 1
Foreword: In this part of the series, I explain PHP conditional structures.
By: Chrysanthus Date Published: 17 Oct 2018
Introduction
The if Structure
In PHP, there is a reserved word, which is “if”. The “if” must be in lowercase. This is used to check if a condition is true. If it is true, one or more statements are executed. I now present an example. Consider the following code:
<?php
$hisVar = 20;
if ($hisVar == 20)
{
echo "I am studying PHP.";
}
?>
In the code, you have the statement that assigns the value 20 to the variable, $hisVar. Then you have the “if” structure. The if-structure begins with the reserved word, “if” and ends with the curly brace, }. Here, there is no semicolon after, }. What goes inside the parentheses is the condition. The statements to be executed are in the braces (curly brackets). The if-structure is not an ordinary statement, so it does not end with a semicolon. The whole if-structure (with the parentheses and curly braces) can be called the if-statement.
If the condition is correct, PHP will replace it with, true, internally; you do not see that. If it is wrong, PHP will replace it with, false, internally.
In the above code, 20 was assigned to, $hisVar. So, $hisVar equals 20. In the condition the equal sign is two assignment operators: one just next to the other. The if-structure above can be read like this: if $hisVar equals 20 then display, "I am studying PHP.". Since we assigned the value 20 to $hisVar, the condition of the if-structure is true. So the statement in the curly braces is executed. Try the above code (you have to give it any file name with the extension .php and save it in the home directory of localhost).
You can have more than one statement in the curly braces of the if-structure. If the condition is true, all the statements in the curly braces will be executed.
else
In the above code, the statement(s) in the curly braces is(are) executed if the condition is true. What if it were false? It would be false if we never assigned 20 to $hisVar. If it were false, nothing will happen; that is, the statement(s) in the curly braces will not be executed. There is an else sub structure you can attach to the if-statement. The else part is similar in coding to the if part. However, its block (curly braces) is executed when the if’s condition is false. The else part does not have any condition. Try the following code:
<?php
$hisVar = 36;
if ($hisVar == 20)
{
print 'I am studying PHP';
}
else
{
print 'I am doing something else';
}
?>
In this code, a value of 36 is assigned to $hisVar. In the if-condition, we test if $hisVar is equal to 20. So the condition returns false, and the statement(s) in the else block is (are) executed. Note how the else section has been typed. Also note that else is a reserved word.
You may have more than one test to make in a particular situation or for the same variable. In this case you include the “elseif” reserved word as in the following code. Read and try it.
<?php
$hisVar = 1000;
if ($hisVar == 10)
{
echo 'Value is small';
}
elseif ($hisVar == 100)
{
echo 'Value is medium';
}
elseif ($hisVar == 1000)
{
echo 'Value is large';
}
?>
A value of 1000 is assigned to $hisVar. The if-elseif coding will test if $hisVar is 10; if it is (which it is not) the corresponding block will display 'Value is small'. The code will then test if $hisVar is 100; if it is (which it is not), the corresponding block will display, 'Value is medium'. The code will then test if $hisVar is 1000; if it is, the corresponding block will display, 'Value is large'. With the if-elseif coding, only one of the blocks can be executed; that is, only one of the conditions can be true (the rest should be false).
In the if-elseif structure, the very first line must be the if-condition; the rest are elseif conditions. The elseif reserved word takes a condition, but the else reserved word never takes a condition.
Always remember this: the if-elseif structure is used only for situations where only one of the conditions is satisfied (is true). At the limit, no condition may be satisfied (no condition may be true).
elseif can also be written as “else if”.
Default Condition
What about the situation for an if-elseif coding where none of the conditions is true? For that situation you will need to report (inform the user) something. This is an opportunity to give some default answer. You do this by simply adding the else (no condition) section at the end of the if-elseif structure. The following code illustrates this:
<?php
$hisVar = 10000;
if ($hisVar == 10)
{
echo 'Value is small';
}
elseif ($hisVar == 100)
{
echo 'Value is medium';
}
elseif ($hisVar == 1000)
{
echo 'Value is large';
}
else
{
echo '$hisVar is very large';
}
?>
Try the above code, if you have not already done so. At the start of the code, 10,000 is assigned to the variable. Note that when you are applying numbers with more than 3 digits, you do not use commas (you type 10000 and not 10,000). In the code, none of the conditions is satisfied, so the last block, which does not have any condition (which is the else part), is executed. Read through the code again, to appreciate this.
The complete syntax for the if-statement is:
if (condition)
{
statements
}
elseif (condition)
{
statements
}
elseif (condition)
{
statements
}
- - -
else
{
statements
}
Quotation Marks
If your value is a number in the condition, you do not need to have it in quotes. However, if it is a string, you need to have it in quotes.
Case Sensitivity
In PHP, certain words are case sensitive and others are not. The reserved words, if, else, and elseif, are not case sensitive.
That is it for this part of the series. Let us stop here and continue in the next part.
Chrys
Related Links
Basics of PHP with Security ConsiderationsWhite Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links