Ad

Display Font Awesome Icons In Php If Clause

- 1 answer

I want to display font awesome icons depending on a condition of two php variable. For example

if($a > $b) display an arrow up icon else an arrow down icon.

Thanks in advance.

Ad

Answer

In my opinion there's 2 ways to implement this, i don't know what the implementation is for. But in its most simple form it would look like this:

<?php

$a = 1;
$b = 0;

if($a > $b){
 echo '<i class="fa-solid fa-arrow-down"></i>';   
}
else{
 echo '<i class="fa-solid fa-arrow-up"></i>';   
}
?>

You could also do it in 1 line this is called a Ternary Expression, this is useually done with simple if statement, to make the code look cleaner.

<?php

$a = 1;
$b = 0;

echo $a > $b ? '<i class="fa-solid fa-arrow-down"></i>'  : '<i class="fa-solid fa-arrow-up"></i>';
?>

As you can see the second option is cleaner, but it can get messy with longer expressions so beware.

Ad
source: stackoverflow.com
Ad