SyntaxStudy
Sign Up
PHP PHP Conditionals & Loops
PHP Beginner 1 min read

PHP Conditionals & Loops

Conditionals & Loops

if / elseif / else

Execute blocks based on conditions.

match (PHP 8)

Strict-typed replacement for switch. Always returns a value.

Loops

  • for($i=0;$i<10;$i++)
  • while(condition)
  • foreach($arr as $val)
  • foreach($arr as $key => $val)
Example
<?php
$score = 85;
if ($score >= 90)       echo "A";
elseif ($score >= 80)   echo "B";
elseif ($score >= 70)   echo "C";
else                    echo "Below C";

// match (PHP 8+)
$status = match(true) {
    $score >= 90 => "Excellent",
    $score >= 70 => "Good",
    default      => "Needs work",
};

// foreach
$langs = ["PHP", "Python", "JS"];
foreach ($langs as $i => $lang) {
    echo "$i: $lang
";
}
?>