PHP If Statement
You can use the PHP If statement to execute code based on condition(s). This statement is also called a conditional statement or the conditional structure. You can make decisions and execute code only when the conditions are met.
Syntax
Let’s look at the simple syntax of the statement.
if(condition)
{
// PHP Statements;
}
The PHP code in the if block will be executed when the condition is met, i.e., evaluates to true.
Example
Simple if statement example:
<?php
// PHP conditional statement
if ($age>=18) {
echo " Adult.";
} else {
echo "Minor.";
}
Another example
<!DOCTYPE html>
<!--
PHP If Statement Demo
-->
<html>
<head>
<meta charset="UTF-8">
<title>PHP If Block</title>
</head>
<body>
<h1> PHP If Block </h1>
<?php
//PHP If Block
$a = 120;
$b = 30;
if($a > $b)
{
//This block will execute only if a > b
echo "a= $a";echo "<br/>";
echo "b= $b";echo "<br/>";
echo "a is greater than b";
}
?>
<br/>
<h1>PHP Tutorials </h1>
<h3>www.TestingDocs.com</h3>
</body>
</html>
—
PHP Tutorials
PHP Tutorials on this website:
More Information on PHP