Friday, February 28, 2014

Conditional Logic in PHP

This week's post is a bit shorter, due to number of midterms that have demanded my attention away from learning PHP. However, I think that it would be at least helpful to share a few comments on conditional statements within PHP.

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!";
  }

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!!";
  }

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#.






No comments:

Post a Comment