Conditional commands and their use if else elseif in php

When the program is intended, after reaching a certain point, to take a specific path in certain cases and a different path if the execution conditions differ, the set of instructions is used:

if, else and elseif

The basic structure of this type of instructions is as follows:

if (Condition)
{
Instruction 1;
Instruction 2;
}
else
{
Instruction A;
Instruction B;
}

When the execution reaches the point where the if statement is, the program will verify the fulfillment or not of the condition. If the condition is true, instructions 1 and 2 will be executed, otherwise instructions A and B will be executed.

In cases where the conditions are various, the if can be used in a way called nested or ringed, as follows:

if (Condition 1)
{
Instruction 1;
Instruction 2;
}
else
{
if (Condition 2)
{
Instruction A;
Instruction B;
}
else
{
Instruction X;
Instruction Z;
}
}

In this way you can enter as many conditions as you want within the main condition. A variant of this system is using the elseif statement, which allows an additional condition to be entered on a single line:

if (Condition 1)
{
Instruction 1;
Instruction 2;
}
elseif (Condition 2)
{
Instruction A;
Instruction B;
}
else
{
Instruction X;
Instruction Z;
}

For the conditions take into account that we have the following operators:
== to see if one variable equals another.
! = different.
> = greater or equal.
> older.
<= less than or equal
<minor

The best way to understand this conditional structure is through examples. The first thing we will consider is to generate a random value (that is, the machine is chosen at random, such as extracting a ball from a lottery box) between 1 and 10. Then we will show a message if it is less than or equal to 5 or if it is greater than 5.
The complete program is:

<html>
<head>
<title> Problem </title>
</head>
<body>
<? php
$ value = rand (1.10);
echo “The value drawn is $ value <br>”;
if ($ value <= 5)
{
echo “Is less than or equal to 5”;
}
else
{
echo “Is greater than 5”;
}
?>
</body>
</html>

It is important to remember that the if condition must always be enclosed in parentheses. If the if condition is verified true (that is, the number drawn is less than or equal to 5), it executes the first block that is enclosed in curly braces. If the if condition is verified false, the block between curly braces that is after the else is executed.
We generate the random value by calling the random function, passing it the minimum and maximum value:

$ value = rand (1.10);

We print the value generated in order to control the result:

echo “The value drawn is $ value <br>”;

In the first problem we have only two possible paths, the value is less than or equal to five or it is greater.In a second example we will show how to arrange a conditional if nested structure.
The problem is as follows: Generate a random value between 1 and 100. Then show if it has 1,2 or 3 digits.
As we can see we are in the presence of a problem that has three possible paths. The value can be 1 digit, 2 digits, or 3 digits. If we think a little, we can identify that for it to have a digit, a value between 1 and 9 must be generated, for it to have two digits, it must be between 10 and 90, and finally, to have 3 digits, it must be the value 100.
The page with the respective program is:

<html>
<head>
<title> Problem </title>
</head>
<body>
<? php
$ value = rand (1,100);
echo “The value drawn is $ value <br>”;
if ($ value <= 9)
{
echo “Has a digit”;
}
else
{
if ($ value <100)
{
echo “It has 2 digits”;
}
else
{
echo “It has 3 digits”;
}
}
?>
</body>
</html>

It is important to note how the second if structure is contained within the else keys of the first if. In other words, if the random value is less than or equal to 9, the block of the true of the first if is executed and therefore the if nested in the else is not executed. On the contrary, if the condition of the first if is verified false, the block of the else of the first if is executed, it contains an if structure with its blocks of true and false.

For illustrative purposes, the same example solved with the if / elseif structure will be:

<html>
<head>
<title> Problem </title>
</head>
<body>
<? php
$ value = rand (1,100);
echo “The value drawn is $ value <br>”;
if ($ value <= 9)
{
echo “Has a digit”;
}
elseif ($ value <100)
{
echo “It has 2 digits”;
}
else
{
echo “It has 3 digits”;
}
?>
</body>
</html>

If you have a lot of practice with other programming languages ​​this nested if structure will be more suitable, in case you have started programming recently it will be convenient to choose and practice the first form.

This article was written by admin

Leave a Reply