IF condition in PHP
The structure of the if can have some variants, the simplest of all is the following.
If (si…)
Many times we want our code to be able to automatically decide if a block of code is executed, or not, depending on something that has happened (the condition to be evaluated may be something that has happened with the data coming from the user, that he have clicked on one option or another, that a variable has a value, or not, that the application has been reached by one method or another, etc.)
Some examples: if the password sent by the user is the same as the one defined in our code, we show you a special content. If it's Friday, we show a phrase wanting a good weekend. If the user chose to see the category "A" of products, we will show the list only of those products and not of others.
We continually encounter this type of situations in real life, for example: "if it's raining, I carry the umbrella"; "If it's night, I light a lamp." It is a matter of simple logic.
To provide our code with the ability to "assess the circumstances" and its subsequent automated decision, in PHP we have the conditional if, which allows us to evaluate if a circumstance is "true", if it happened and, depending on the result of that evaluation , it allows us to execute a block of code.
That is to say: we can make the execution of a block of code conditional (hence the name), which only in the case of the planned condition is true, is executed; otherwise, nothing will happen, the code following that conditional will continue to run.
The structure of the if can have some variants, the simplest of all is as follows:
if(Condition to evaluate) { Block that executes only if that condition turns out to be true }
and if it is not true, nothing happens.
Applied to real life, it would be like this in the case of rain:
if(it rains) { Take umbrellas }
Or what is the same, but plotted as a diagram:
This means that those occasions wrapped between the conditional keys, will not always be carried out (we do not go out every day with the umbrella!), But those blocks of code will only be executed when the condition that is evaluated is true (it rains, in this case). Otherwise, the orders that follow will continue to be executed.
Let's see an example in PHP:
<?php
if ($_POST["password"]=="superagente86") {
echo "<h1>Maxwell, attend the shoe phone we are calling from control </h1>";
}
?>
In this example, we assume that from another page we have sent, using a form with method = "post", an input called password. Only if within this field we have sent the data "superagent86", the condition will be true, and the echo will write the message inside the page.
Otherwise (that the user has written any other text), the condition will be evaluated as false, and the block of code wrapped between the keys of the if will not be executed, therefore, the echo will not write anything. This echo, the code wrapped between the keys of the if, is a code whose execution is conditional; that is, it is subject to the result of evaluating whether the condition is true at the time this page is processed.
We can try it to see it in action and also enter any other value within the password field, in this way we will see how the conditional block is not executed, and we will remain without knowing the secret message.
ChatGPT Free
Ask questions on any topic
CITE ARTICLE
For homework, research, thesis, books, magazines, blogs or academic articles
APA Format Reference:
Delgado, Hugo. (2019).
IF condition in PHP.
Retrieved Oct 06, 2024, from
https://disenowebakus.net/en/conditional-if-php