if construct is one of the most important features of PHPoC. It allows for conditional execution of code fragments.
Structure of if
Syntax | Description |
---|---|
if (expr) stmt; |
Execute stmt if expr is TRUE, otherwise skip stmt. |
<?php
$var1 = $var2 = 1;
if($var1 == $var2) // expression is TRUE
echo "var1 and var2 are equal"; // statement will be executed
?>
[result]
var1 and var2 are equal
<?php
$var1 = 1;
$var2 = 2;
if($var1 < $var2)
{ // grouping by curly braces
echo "var1 is smaller than var2";
echo "\r\nbye!";
} // grouping by curly braces
?>
[result]
var1 is smaller than var2
bye!
<?php
$var1 = $var2 = 1;
$var3 = 2;
if($var1 == $var2) // expression is TRUE
{
if($var1 < $var3) // expression is TRUE
echo "var1 and var2 are equal"; // statement will be executed
}
?>