CodeIgniter 4 – Conditional Block and Loops

Conditional Blocks 

<html>

<head>
</head>

<body>
    <p>
        <?php 
            $value = 20; 
            
            <!-- Using a Curly Bracket -->
            if ($value > 2) {
                echo "Value is greater than 2";
            } else {
                echo "Value is lesser than 2";
            }                
            
            <!-- Using a Colon -->
            if ($value > 2):
                echo "Value is greater than 2";
            else:
                echo "Value is lesser than 2";
            endif;     
        ?>
    </p>
</body>

</html>

Loops

Controllers > Site.php

public function index() 
{

    $name = "Online Tutorial"
    $author = "Mj Octavio"
    $email = "support@programm3r.com"
    $employees = ["John", "Mary", "Sandra"]
    
    return view("site/index", compact(
        "name",
        "author",
        "email",
        "employees")
    );
    
}

Views > site > index.php

<html>

<head>
</head>

<body>
    <p>
        <?php 
            echo "br/>";
            
            <!-- Using Curly Bracket -->
            foreach($employees as $employee) {
                echo $employee . "br/>";
            }
            
            <!-- Using Colon -->
            foreach($employees as $employee):
                echo $employee . "br/>";
            endforeach;
            
        ?>           
    </p>
</body>

</html>