Last Updated:

Conditions if, elseif, else in PHP

Conditions if, elseif, else in PHP

The if construct and its elseif and else components form a fundamental part of the programming languages to which PHP belongs. With its help, you can compare the values of variables and perform various actions depending on a particular result. The structure of recording logical operations here is identical to the format presented in the C language, but even in the absence of experience, you can quickly understand this issue.

How parts are translated

 

It's as simple as that:

  • if – if. Always facing the rest of the conditions. On the same line with it in parentheses there should be a comparable expression, as well as an opening curly bracket, and before the closing one - an action that should be performed with full compliance;
  • elseif – otherwise if. You should specify in the case when the first condition turned out to be incorrect, but you need to add another comparison. You can also write a component in two words: else if. This will lead to the same result. In one condition, it can be present in unlimited quantities;
  • else is different. It is triggered when the other conditions turned out to be false (invalid).

A mandatory part is only if. In the absence of other comparison parameters, false conditions will not lead to anything – for example, a change to a variable will not be made.

In which cases it is used

As mentioned above, if is one of the main parts of PHP, thanks to which the range of its application is extensive. For example, in this way you can display different information depending on the results obtained in the API, the values returned by the external script, and in other cases, the number of which is simply countless.

From this description, it may seem that if, elseif and else are complex constructions, but this is absolutely not the case. You can make sure that there are no difficulties thanks to the examples given in this text.

Alternate syntax

 

If you plan to work with the output of data that contains quotation marks, parentheses and other components that can conflict with the backend code, it is recommended that you follow a syntax that is different from the usual for PHP.

Despite its existence, it is not necessary to use it - it is enough to escape the above-mentioned characters by adding a backslash (\) before each of them, but this action will take a particularly long time when try > <ing to display a large code fragment.

PHP Logical Operators

 

To write multiple expressions to a condition, logical operators are used, which are translated as follows:

  • and (&&) – "and";
  • or (||) – "or";
  • ! - Denial;
  • xor is exclusive or.

In parentheses to the paragraphs are their higher priority variants used in complex designs. That is, instead of if($a== 4 and $b==1), you can specify if($a == 4 && $b== 1). Although it is not practical to prioritize in such situations, this code will also lead to an identical result.

The last paragraph might cause some difficulties due to the absence of the word "xor" in English. This operator can be described as "and, but not both at the same time". Do not confuse the elements of the list with conditional operators - they are the subject of the material as a whole.

Ternary operator

 

The use of this feature of PHP is controversial, because in addition to the time savings it provides on writing, the readability of the code deteriorates. But do not worry - the ternary operator is effective for small comparisons and records. It is an abbreviated form of if-elseif-else.
The operator is described in more detail in the examples section.

Zero Coalescence Operator

 

A major language update, called PHP7, brought a new operator written with two question marks (??) and, like the previous one, designed to shorten the code. They are supposed to work with the isset() function, which determines whether the variable is null. If its value is different from this one, true will be returned, otherwise false.

Examples

Simple number comparison

<?
$a = 123;
$b = 123;

if($a == $b) {
echo"AetoB";
} else {
echo"And this is not B";
}
?>

The result of the execution will be the conclusion "A is B".

Compare numbers with elseif

<?
$a = 1;
$b = 2;
$c = 3;
if($a == $b) {
echo"A is B";
} elseif($b < 3) {
echo"3 is greater than B";
} elseif($c == 1) {
echo"C is 1";
} else {
echo "Something else...";
}
?>

As a result, "3 more than B" will be returned. The example not only explains the logic of how elseif works, but also demonstrates the possibility of using this operator in the amount of more than one.

Comparison with alternate syntax

<?
$a = 1;
$b = 2;
if($a == $b) {
echo"<b style=\"color: red\">A is B</b>";
} else {
echo"<b style=\"color: darkgreen\">A is not B</b>";
}

The condition above is written in normal syntax. As you can see, the quotation marks are preceded by a reverse slash (\), which serves to escape quotation marks - otherwise the code simply will not compile.

Despite the effectiveness of this method, the following form of writing is more suitable for displaying HTML tags with the properties specified for them and is especially relevant when working in parallel with other programming languages - for example, JavaScript.

<?
$a = 1;
$b = 2;
if($a == $b):?>
<b style="color: red">A is B</b>
<?else:?>
<b style="color: darkgreen">A is not B</b>
<?endif?>

As you can see, there is a previously unmentioned endif operator. It is used only in this syntax and serves to provide the compiler with an understanding of where the condition terminates. Here, the tags are issued without any escaping or even the echo command.

In both cases, the screen will display the inscription "And this is not B", written in dark green.

Double comparison

<?
$a = 1;
$b = 1;
if($a == $band $b !== 1) {
echo"A and B are equal to each other and B is not equal to 1.";
} else {
echo"A and B are not equal to each other and/or B is equal to 1.";
}
?>

The result is the display of the inscription "A and B are not equal to each other, and / or B is 1".

Comparisons with the ternary operator

The following example compares whether the value of the variable $a is less than that of $b. This results in the definition of the variable $c:

<?
$c = ($a < $b) ? $a : $b;
?>

Comparisons with the zero coalescence operator

The following example assumes that there is a form where the value of the name field was passed by the POST request.

<?php
$name = $_POST[‘name’] ?? ‘unknown’;
?>

Here, the variable $name becomes the value of the name field. If its length is zero characters or null, unknown is written in the $name.

Total

These are all the main applications of the if-elseif-else construct, which can be used both in simple operations and in processing large amounts of data.