image


Valora:  2.5/5
Inicio » PHP » PHP Tutorial » Statements




Conditional Statements

Conditional statements allow us to execute or not certain instructions depending on the resulting assessment of a condition. The most frequent statements are the "if" statement and the "switch" statement.

Statement if ... else
<?php
   if (condición)
   {
      Sentencias a ejecutar cuando la
      condición es cierta.
   }
   else
   {
      Sentecias a ejecutar cuando la
      condición es falsa.
   }
?>

The "if" statement executes a series of codes or another depending on the condition statement that is applied. It is probably the most important statement in any programming language.

<!-- PHP Tutorial WebEstilo.com -->
<html>
<head>
   <title>PHP Example</title>
</head>
<body>
<?php
   $a = 8;
   $b = 3;
   if ($a < $b)
   {
      echo "a is less than b";
   }
   else
   {
      echo "a is greater than b";
   }
?>

</body>
</html>

Execute View code

In this example the condition is not true, so the part of the code corresponding to "else" is executed.



Statement switch ... case
<!-- PHP Tutorial WebEstilo.com -->
<html>
<head>
   <title>PHP Example</title>
</head>
<body>
<?php
   $posicion = "up";
   
   switch($posicion) {
      case "up":   // Bloque 1
         echo "value of the variable";
         echo " is up";
         break;
      case "down":   // Bloque 2
         echo "value of the variable";
         echo " is down";
         break;
      default:   // Bloque 3
         echo "value of the variable";
         echo " is neither of the up and down";
   }
?>

</body>
</html>

Execute View code

With the "switch" sentence we can execute one set of instructions or another depending on the value of a variable; in the aforementioned example depending on the value of the variable $position, block 1 is executed when the value is "up", block 2 is executed when the value is "down", and block 3 if it is neither of the previous values.






WebEstilo.com - Introduzca su e-mail y conozca las novedades. No hacemos Spam.
Google
  Web WebEstilo.com   
 
Valid HTML 4.01!
Última modificación:24 de Diciembre de 2004. Spain - España.
© 1998-2004 por Joaquin Gracia. Todos los derechos reservados.