Estructures de control. Bifurcacions i bucles en PHP
ex02.php
<html>
<head>
    <meta content="text/html; charset=UTF-8" http-equiv="content-type">
    <title>Estructures de control. Bifurcacions i bucles</title>
</head>  
<body>  
 <?php
 
function presentaCodiFont($codiFont){
     if(!
file_exists($codiFont)){
        return -
1;
     }
     echo 
"<B>$codiFont<HR><FONT size = 3>";
     
show_source($codiFont);
     echo 
"</font></B><P>";
     echo 
"<A href=\"$codiFont\">Executa-ho</a><HR>";
     return 
0;
 }
 echo 
"<A href=\"ex02.pdf\">Estructures de control. Bifurcacions i bucles en PHP</a><HR>";
 
presentaCodiFont("ex02.php");
 
presentaCodiFont("ex02-1.php");
 
presentaCodiFont("ex02-2.php");
 
presentaCodiFont("ex02-3.php");
 
presentaCodiFont("ex02-4.php");
?>  
</body>
</html>

Executa-ho


ex02-1.php
<html>
<head>
    <meta content="text/html; charset=UTF-8" http-equiv="content-type">
    <title>Bifurcació if-else</title>
</head>
<body>
<h2>Bifurcació if-else</h2>
<?php
    $mesActual
=date('F'time());
    echo 
"\$mesActual : $mesActual <BR>";
    if (
$mesActual == 'August'){
        echo 
"<p>És agost, i per això fa tanta calor.</p>";
    }else{
        echo 
"<p>No és agost, almenys no estem patint tanta calor.</p>";
    }
?>
</body>
</html>

Executa-ho


ex02-2.php
<html>
<head>
    <meta content="text/html; charset=UTF-8" http-equiv="content-type">
    <title>Bucles</title>
</head>
<body>
<h2>Bucles</h2>
<?php
     
echo "<p>\n";
     
    
$comptador 1;
    while (
$comptador 10){
      echo 
'abc ';
      
$comptador++;
    }
     
    echo 
"</p>\n";
    echo 
"<p>\n";
     
    
$comptador 1;
    do{
      echo 
'xyz ';
      
$comptador++;
    } while (
$comptador 10) ;
     
    echo 
"</p>\n";
     
    for (
$x=1$x<10$x++){
      echo 
"$x ";
    }
    
//L'espai a dins de "" és necessari per a separar els números 
     
    //Genera llista ordenada
    
echo "\n<ol>";
    for (
$x='A'$x<'G'$x++){
      echo 
"<li>Item $x</li>\n";
      }
    echo 
"\n</ol>";
    
//Adona't que les lletres es poden emprar al bucle en comptes dels números
?>
</body>
</html>

Executa-ho


ex02-3.php
<html>
<head>
    <meta content="text/html; charset=UTF-8" http-equiv="content-type">
    <title>Bucle for</title>
</head>
<body>
<h2>Quadrats dels números 1-12</h2>
<?php
    
for ($x=1$x<=12$x++){
          
$resultat $x $x;
          echo 
"$x * $x = $resultat <br />\n";
    }
?>
</body>
</html>

Executa-ho


ex02-4.php
<html>
<head>
    <meta content="text/html; charset=UTF-8" http-equiv="content-type">
    <title>Bucles for niats</title>
</head>
<body>
<h2>Bucle for niat per fer la taula de multiplicar</h2>
<?php
    
//Genera una taula HTML 
    
echo "<table border=\"1\">";
       
    
// Genera taula de dades presentant els números 1-7 multiplicats 
    // per sí mateixos començant per les files
    
for ($fila=1$fila<=7$fila++){
      echo 
"<tr>\n";
      
//Genera cada entrada a la fila per crear les columnes
      
for ($col=1$col<=7$col++){
        
// Primer es fa el càlcul
        
$x=$col $fila;
        
// Es trameten els valors a la taula formatada
        
echo "<td>$x</td>\n";
      }
      echo 
"</tr>";
    }
    echo 
"</table>";
?>
</body>
</html>

Executa-ho