if..else construct and ternary operation in php

Conditional constructions allow you to direct the work of the program, depending on the condition, along one of the possible paths. And one such construct in PHP is the if..else construct.

if..else construct

The construction if (condition) checks the truth of some condition, and if it turns out to be true, then the block of expressions after the if is executed. If the condition is false, that is, equals false, then the if block is not executed. For example:

<?php
$a = 4;
if($a>0){
    echo "Variable a is greater than zero";
}
echo "<br>end of program execution";
?>

An expression block is delimited by curly braces. And since in this case, the condition is true (that is, it is equal to true): the value of the $a variable is greater than 0, the block of instructions in curly braces will also be executed. If the value of $a were less than 0, then the if block would not be executed.

If the if the block contains only one statement, then you can omit the curly braces:

<?php
$a = 4;
if($a>0)
    echo "Variable a is greater than zero";
    echo "<br>end of program execution";
?>

You can put the whole structure in one line:

 
if($a>0) echo "Variable a is greater than zero";

In this case, only the statement applies to the if block echo “Variable a is greater than zero”;

else

The else block contains statements that are executed if the condition after the if is false, that is, it is equal to false:

 <?php
$a = 4;
if($a > 0){
    echo "Variable a is greater than zero";
}
else{
    echo "Variable a is less than zero";
}
echo "<br>end of program execution";
?>

If $a is greater than 0, then the if block is executed, if not, then the else block is executed.

Since there is one instruction in both blocks here, it was also possible not to use curly braces to define blocks:

if($a > 0)
    echo "Variable a is greater than zero";
else
    echo "Variable a is less than zero";
elseif

The elseif construct introduces additional conditions into the program:

 
    $a = 5;
if($a>0){
    echo "Variable a is greater than zero";
}
elseif($a < 0){
    echo "Variable a is less than zero";
}
else{
    echo "Variable a is zero";
}

You can add multiple blocks elseif. And if none of the conditions in the if or elseif is true, then the else block is fired.

Condition Definition

Above, comparison operations were used as a condition. However, in reality, any expression can be used as a condition, not just one that returns true or false. If the passed expression is 0, then it is interpreted as a value false. Other values ​​are treated as true:

if (0) {} // false
if (-0.0) {} // false
if (-1) {} // true
if ("") {} // false (empty string)
if ("a") {} // true (non-empty string)
if (null) {} // false (no value)

Alternate if syntax

PHP also supports an alternative syntax for the if..else, which replaces the opening curly brace with a colon and ends the whole construct with the keyword endif.

 
$a = 5;
if($a > 0):
    echo "Variable a is greater than zero";
elseif($a < 0):
    echo "Variable a is less than zero";
else:
    echo "Variable a is zero";
endif;

Combined HTML and PHP mode

We can also write the construct if..else in a different way, switching inside the construct to the HTML code:

<!DOCTYPE html>
<html>
<head>
<title>easywptutorials.com</title>
<metacharset="utf-8" />
</head>
<body>
<?php
$a = 5;
?>

<?php if ($a > 0) { ?>
<h2>Variable a is greater than zero</h2>
<?php} ?>

</body>
</html>

In this case, the condition itself is specified in a separate php: block

<?php if ($a > 0) { ?>

. It is important that this block contains only the opening curly brace “{“.

The if construct ends with another PHP block that contains a closing curly brace:

<?php } ?>

Between these two php blocks is the code that is displayed on the HTML5 page if the if condition is true. Moreover, this code represents exactly the HTML code, so you can place various HTML elements here, as in this case, the element

Optionally, you can add expressions else and elseif:

<!DOCTYPE html>
<html>
<head>
<title>easywptutorials.com</title>
<metacharset="utf-8" />
</head>
<body>
<?php
$a = -5;
?>

<?php if ($a > 0) { ?>
<h2>Variable a is greater than zero</h2>
<?php } elseif($a < 0) { ?>
<h2>Variable a is less than zero</h2>
<?php } else { ?>
<h2>Variable a is zero</h2>
<?php} ?>
</body>
</html>

You can also use an alternative syntax:

 
<!DOCTYPE html>
<html>
<head>
<title>easywptutorials.com</title>
<metacharset="utf-8" />
</head>
<body>
<?php
$a = 0;
?>

<?php if ($a > 0): ?>
<h2>Variable a is greater than zero</h2>
<?php elseif($a < 0): ?>
<h2>Variable a is less than zero</h2>
<?php else: ?>
<h2>Variable a is zero</h2>
<?php endif; ?>
</body>
</html>

Ternary operation

The ternary operation consists of three operands and has the following definition: [first operand – condition] ? [second operand] : [third operand] . Depending on the condition, the ternary operation returns the second or third operand: if the condition is true, then the second operand is returned; if the condition is false, then the third one. For example:

$a = 1;
$b = 2;
$z = $a < $b ? $a + $b : $a - $b;
echo $z;

If the value of $a is less than $b and the condition is true, then $z will be equal to $a + $b. Otherwise, the value of $z will be equal to $a – $b