You get a bonus - 1 coin for daily activity. Now you have 1 coin

Conditional statements in PHP

Lecture



Conditional operators are perhaps the most common constructs in all algorithmic programming languages. Consider the basic conditional operators of the PHP language.

If construct

The if construct has the same syntax as if in the C language:

<?php
if (логическое выражение) оператор;
?>

According to PHP expressions, the if construct contains a logical expression. If the logical expression is true ( true ), the statement following the if construct will be executed, and if the logical expression is false ( false ), then the statement following the if statement will not be executed. Here are some examples:

<?php
if ($a > $b) echo "значение a больше, чем b";
?>

In the following example, if the variable $ a is not equal to zero, the string "the value of a true (true)" will be displayed:

<?php
if ($a) echo "значение a истинно (true) ";
?>

In the following example, if the variable $ a is zero, the string "the value of a is false (false)" will be displayed:

<?php
if (!$a) echo "значение a ложно (false) ";
?>

Often you will need a block of statements that will be executed under a certain conditional criterion, then these statements must be placed in curly braces {...} Example:

<?php
if ($a > $b) {
echo "a больше b";
$b = $a;
}
?>

The above example will display the message, "a is greater than b", if $ a > $ b , and then the variable $ a will be equated to the variable $ b . Note that these statements are executed in body structure if .

Else construct

Often there is a need to execute statements not only in the body of the if construct, if any condition of the if construct is fulfilled, but also if the condition of the if construct is not met. In this situation, it is impossible to do without the else construct . In general, such a construct will be called an if-else construct .

The if-else syntax is:

if (логическое_выражение)
инструкция_1;
else
инструкция_2;

The action of the if-else construct is as follows: if logical_expression true then executed
instruction_1 otherwise instruction_2 . As in any other language, the else construct can be omitted, in which case nothing is done to get the proper value.

If a instruction_1 or instruction_2 must consist of several teams then
they, as always, are enclosed in braces. For example:

<?php
if ($a > $b) {
echo "a больше, чем b";
} else {
echo "a НЕ больше, чем b";
}
?>

The if-else construct has another alternative syntax:

if (логическое_выражение):
команды;
elseif(другое_логическое_выражение):
другие_команды;
else:
иначе_команды;
endif

Pay attention to the location of the colon ( :) ! If you skip it, an error message will be generated. And yet: as usual, the elseif and else blocks can be omitted.

Elseif construction

elseif is a combination of if and else constructs . This construct extends the conditional if-else construct .

Here is the elseif construct syntax:

if (логическое_выражение_1)
оператор_1;
elseif (логическое_выражение_2)
оператор_2;
else
оператор_3;

A practical example of using the elseif construct:

<?php
if ($a > $b) {
echo "a больше, чем b";
} elseif ($a == $b) {
echo "a равен b";
} else {
echo "a меньше, чем b";
}
?>

In general, the elseif construction is not very convenient, therefore it is used less frequently.

created: 2016-01-25
updated: 2021-03-13
138



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)