IFCD0210/Practicas/Practicas_PHP/codigo/INTRO2_PHP_BUCLES.php
2024-02-06 22:37:14 +01:00

50 lines
1.2 KiB
PHP
Executable File

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Intro2 PHP</title>
</head>
<body>
<h1>Bucles</h1>
<?php
// foreach
$animalesFantasticos = ['fénix', 'dragón', 'grifo', 'pegaso', 'cerbero'];
foreach ($animalesFantasticos as $animal) {
echo $animal . ' ';
}
echo '<br>';
//si necesitamos la key
foreach ($animalesFantasticos as $posicion => $animal) {
echo "El animal $animal ocupa la posición $posicion";
echo '<br>';
}
//range crea un array entre un rango especificado
//range($inicio, $fin, $pasos);//Esquema
foreach (range(1, 5) as $num) {echo $num;};
//for
//Estructura for (variable inicio; condicional; incremento) {...}
echo '<br>';
for ($i = 0; $i < 10; $i++) {
echo $i;
}
echo '<br>';
//while
// Estructura while (condicional) {...}
$i = 1;
while ($i < 12) {
echo $i++;
}
echo '<br>';
//do-while
// Estructura do {...} while (condicional) una vez siempre
$i = 1;
do {
echo $i++;
} while ($i < 10);
echo '<br>';
?>
</body>
</html>