At the heart of conditional logic in PHP is the If statement
The logic behind the if statement in PHP matches that of most other languages. If this condition is met, then execute this code.
The syntax is as follows:
if ($t<"10")
{
echo "Hello World!";
}
{
echo "Hello World!";
}
In this particular IF statement, the "Hello World" text will only be displayed if the variable t is less than 10.
If statements are sometimes supplemented with else and elseif clauses to help specify alternate routes of action when the "if" test is false.
if ($x=="10")
{
echo "Hello World!";
}
elseif ($x=="20")
{
echo "How are you, World?";
}
else
{
echo "Goodbye World!!";
}
{
echo "Hello World!";
}
elseif ($x=="20")
{
echo "How are you, World?";
}
else
{
echo "Goodbye World!!";
}
In this example, the text "Hello World!" will only be executed if x is equal to 10. If x is not equal to 10, but equal to 20, the text "How are you, World?" will be displayed. If x is not equal to 10 nor 20, the the text "Goodbye World" will be displayed.
What is interesting about the if statement in PHP is that it is absolutely identical to the if statement syntax in Java. So which language copied which? Both languages were debuted 19 years ago in 1995, so it is likely that both were in development around the same time. What is more likly is that both languages borrowed some syntax, including the if statement, from a common predecessor language like C or C#.