Chose Language:
Author: Admin/Publisher |finished | checked

PHP if statement

In this section, we will see the different ways to use the PHP if statement. This control structure allows us to execute code depending on a condition.

In the following sections, we will refer to this condition as an expression. Expressions can be formed with comparison and logical operators to obtain a boolean result.

if

First, we will see the structure of if. The block starts with the if keyword and is delimited with { and }.

The expression will always result in true or false, no matter what is passed; values like "", null or 0 will result in false, on the other hand, values different from this will result in true.

Descripción / Sintaxis
if(expresión){
   //este código será ejecutado
   // si la expresión anterior da como resultado true
}

Another way to express an if statement

Descripción / Sintaxis
if(expresion):
   //este código será ejecutado
   // si la expresión anterior da como resultado true
endif;

The expression is evaluated; if it is true, the code inside the if statement will be executed. If it is false, the code inside the if statement will not be executed, and the program will continue to the next line.

Examples of if statements:

Example 1
<?php
$a = 5;
if($a<7){
    echo "$a es menor que 7";
}
if(a>7){
    echo "$a es mayor que 7";
}
?>

Copy and test the following example to understand the expressions, which you will see throughout this post.

Understanding the expresions
<?php
if(null){
    echo "hola tu !!!";
}else{
    echo "not true";
}
?>

Copy the above code to test different values in the expression, although generally it is not just one value that makes up the expression, but rather the expression is composed of variables being compared using comparison operators as we saw in Example 1.

else

You can extend the if statement with the else statement, in case the expression of if is false, what is inside else will be executed.

Sintaxis / Sintax
if(expresion){
   //codigo cuando expresion es true
}else{
   //codigo cuando expresion es false
}

Else example:
<?php
$a = 10;
if($a<7){
    echo "$a es menor que 7";//si a es < 7
}else{
    echo "a es mayor que 7";// si a es > 7
}
?>

PHP else if and elseif

You can also extend if with the elseif statement, which allows you to check if another expression is met. This statement never goes after else, but it can go before it.

Sintaxis / Sintax
if(expresion){
    //codigo cuando expresion es true
}elseif(expresion2){
    //codigo cuando expresion2 es true
 }else{
    //si ninguna de la expresiones anteriores es true
 }

elseif example:

elseif example
<?php
$a = 7;
if($a<7){
    echo "$a es menor que 7";
}elseif($a == 7){
    echo "$a es igual a 7";
}else{
    echo "$a es mayor que 7";
}
?>

if inside a function

When we use an if inside a function, it is recommended not to use else, because we can do the same action without it. After making an if, we can perform a return, and it will have the same behavior:

Sintaxis / Sintax
function myfuncion(){
   if($expresion){
       return $value1;
   }
   return $value2;
}

The return that returns the $value2 already complies with the behavior of else because if the expression is not met, it will return this value.

The same occurs if we have an else if. It is advisable to avoid the use of else here since it is much more readable.

Sometimes it will be more useful to use switch instead of if-elseif-else. In other cases, if is a better option. If we have a large number of comparisons, it will be convenient to use switch. If we have a few, if will be a better option

This entry is part of the control structures in PHP. You can see in a summarized form more about these at https://blastcoding.com/estructuras-de-control-php/

Reference: https://www.php.net/manual/es/control-structures.if.php

Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category