85 lines
2.5 KiB
PHP
85 lines
2.5 KiB
PHP
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Intro3 PHP</title>
|
|
</head>
|
|
<body>
|
|
<h1>Condicionales</h1>
|
|
<?php
|
|
//IF
|
|
// Estructura if (condición) {...}
|
|
/*Posibles condiciones
|
|
> es mayor que if (1 > 0)
|
|
< es menor que if (1 < 0)
|
|
&& y if (1 > 0 && 67 > 0)
|
|
|| o if (1 > 10 || 67 > 0)
|
|
! no if (!(1 > 0))
|
|
== es igual en valor if ('3' == 3)
|
|
=== es igual en valor y tipo if ('3' === '3')
|
|
!= no es igual if ('Doctor' != 'Who')
|
|
!== no es igual en valor o tipo if ('Doctor' !== 'Who')
|
|
>= es mayor o igual que if (10 >= 10)
|
|
<= es menor o igual que if (10 <= 20)
|
|
<=> -1, 0 y 1 dependiendo de los valores si son superados (10 <=> 20) // 1
|
|
True Verdad if (True)
|
|
False Falso if (False)
|
|
*/
|
|
if (10 > 2 && True && 'PACO' != 'PEDRO') {
|
|
echo 'Entro seguro';
|
|
}
|
|
echo '<br>';
|
|
//else
|
|
//if ('Ghibli' == 'Ghibli') {echo 'Bienvenido'} else {echo 'No eres bien recibido'}
|
|
if ('Ghibli' == 'Ghibli') {
|
|
echo 'Bienvenido';
|
|
} else {
|
|
echo 'No eres bien recibido';
|
|
}
|
|
echo '<br>';
|
|
//if (condición1) { ... } elseif (condición2){ ... } } else { ... }
|
|
//Operador ternario ? :
|
|
// SI SI NO
|
|
// (condicional) ? 'Valor si se cumple' : 'Valor si no se cumple';
|
|
/*Estructura <?php (condicional) ? 'Valor si se cumple' : 'Valor si no se cumple'; ?>*/
|
|
echo (5 > 10) ? 'Es verdad' : 'Es mentira';
|
|
echo '<br>';
|
|
//Switch
|
|
$num=2;
|
|
switch ($num) {
|
|
case 0:
|
|
echo "num es igual a 0";
|
|
break;
|
|
case 1:
|
|
echo "num es igual a 1";
|
|
break;
|
|
case 2:
|
|
echo "num es igual a 2";
|
|
break;
|
|
default:
|
|
echo "No se a que es igual";
|
|
break;
|
|
}
|
|
echo '<br>';
|
|
//Condiciones con strings
|
|
//str_contains (¿Contiene este texto este otro texto?)
|
|
if (str_contains('La duda es uno de los nombres de la inteligencia', 'duda')) {
|
|
echo 'Si está contenido';
|
|
}
|
|
//str_starts_with (¿Empieza este texto con este otro texto?)
|
|
//str_end_with (¿Termina este texto con este otro texto?)
|
|
|
|
//Rand uso aleatorio
|
|
//rand( int $min , int $max )
|
|
|
|
echo '<br>';
|
|
echo rand(), "\n";
|
|
echo '<br>';
|
|
echo rand(), "\n";
|
|
echo '<br>';
|
|
echo rand(1,6), "\n";
|
|
?>
|
|
|
|
</body>
|
|
</html>
|