PHP Conditions

Conditions are used to execute part of a script only if some predefined requirements (conditions) are fulfilled. For example, a condition could be that a date must be after January 1, 2012 or that a variable is greater than 7.

If...


The first type of condition we will look at is documentationif, which has the following syntax:


if (condition) {
  statement
}


Again, the syntax is very close to ordinary English: If a condition is met, then execute something. Let's look at a simple example:


<html>

<head>
<title>Loops </title>
</head>
<body>

<?php

$x = 2;

if ($x > 1) {
  echo "<p>variable $x is greater than 1 </p>";
}

?>

</body>
</html>


if ... else ...


The next type of condition will want to look at is documentationelse , which may be presented in the following form:


if (condition) {
  statement
}
else {
  statement
}


Again, the syntax is very close to ordinary English: if a condition is met execute something or else execute something else.

Example:
 we will use the month number in an documentationif documentationelse condition to find out what season it is:


<html>
<head>
<title>Conditions</title>
</head>
<body>

<?php

if (date ("m") == 3) {
  echo "<p>Now it's spring!</p> ";
}
else {
  echo "<p>I do not know what season it is!</p> ";
}

?>

</body>
</html>

However, there are plenty of ways to improve the condition and make it more precise. Below are listed comparison operators that can be used in the condition:

== Equals
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
!= Not equal to

In addition, there are some logical operators:

&& And
|| Or
! Not

The operators can be used to develop more precise conditions, so now we can expand the above example to include all the spring months:


<html>
<head>
<title>Conditions</title>

</head>
<body>

<?php

if (date("m") >= 3 && date("m") <= 5) {
  echo "<p> Now it's spring!</p> ";
}
else {
  echo "<p> Now it's either winter, summer or autumn!</p> ";
}

?>

</body>
</html>



Let's take a closer look at the extended condition:

date("m") >= 3 && date("m") <= 5

The condition can be translated into:


If the month is greater than or equal to 3,
and the month is less than or equal to 5

if ... elseif ... else...


Using documentationelseif, we can expand the condition and make it work for all months:


<html>
<head>
<title>Conditions</title>

</head>
<body>

<?php

if (date("m") >= 3 && date("m") <= 5) {
  echo "<p>Now it's spring!</p>";
}

elseif (date("m") >= 6 && date("m") <= 8) {
  echo "<p>Now it's summer!</p>";
}

elseif (date("m") >= 9 && date("m") <= 11) {
  echo "<p>Now it's autumn!</p>";
}

else {
  echo "<p>Now is winter!</p>";
}

?>

</body>
</html>

switch ... case

Another way of writing conditions is to use the documentationswitch method:


switch (expression) {

case 1:
  statement
  break;
case 2:
  statement
  break;
default:
  statement
  break;
}

Example:
<html>
<head>
<title>Conditions</title>
</head>
<body>

<?php

switch(date("w")) {

case 1:
  echo "Now it's Monday";
  break;
case 2:
  echo "Now it's Tuesday";
  break;
case 3:
  echo "Now it's Wednesday";
  break;
case 4:
  echo "Now it's Thursday";
  break;
case 5:
  echo "Now it's Friday";
  break;
case 6:
  echo "Now it's Saturday";
  break;
default:
  echo "Now it's Sunday";
  break;

}

?>

</body>
</html>

No comments:

Post a Comment