PHP
This commit is contained in:
127
Practicas/Practicas_PHP/codigo/INTRO3_PHP_FUNCIONES.php
Normal file
127
Practicas/Practicas_PHP/codigo/INTRO3_PHP_FUNCIONES.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Intro3 PHP FUNCIONES</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Funciones en PHP</h1>
|
||||
<form>
|
||||
<?php
|
||||
//--- Declarar
|
||||
/*function nombre_de_funcion(tipo_de_parametro $parametros): tipo_return
|
||||
{ ...
|
||||
return ...;
|
||||
}*/
|
||||
//--- Llamar
|
||||
//nombre_de_funcion($parametros);
|
||||
//--- Estructura correcta
|
||||
/**
|
||||
* Función con educación
|
||||
* @return {string}
|
||||
*/
|
||||
function saludar_ahora(): string
|
||||
{
|
||||
return 'Hola, soy una función';
|
||||
}
|
||||
echo saludar_ahora();
|
||||
echo '<br>';
|
||||
// Hola, soy una función
|
||||
|
||||
|
||||
// --- Nuestros parámetros de entrada pueden tener un valor por defecto.
|
||||
/**
|
||||
* Saluda a una persona
|
||||
* @param {string} - Nombre
|
||||
* @return {string}
|
||||
*/
|
||||
function saludar(string $nombre = 'Anónimo'): string
|
||||
{
|
||||
return 'Hola, persona llamada ' . $nombre ;
|
||||
}
|
||||
echo saludar();
|
||||
echo '<br>';
|
||||
// Hola, persona llamada Anónimo.
|
||||
echo saludar('Picasso');
|
||||
echo '<br>';
|
||||
// Hola, persona llamada Picasso.
|
||||
|
||||
//De forma automática PHP arreglará las incompatibilidades de tipos.
|
||||
function incrementar(int $num): int
|
||||
{
|
||||
return $num + 1;
|
||||
}
|
||||
echo incrementar(4.5);
|
||||
echo '<br>';
|
||||
// 5
|
||||
|
||||
//Podemos declarar el modo estricto para que no admita errores de tipo
|
||||
//declare(strict_types=1);
|
||||
|
||||
//Return con posibilidad de null
|
||||
//function nombre(): ?string {...}
|
||||
|
||||
//Es posible indicar 2 tipos diferentes de parametros
|
||||
//function nombre(): int|string
|
||||
|
||||
// -------------- Algunas funciones de arrays ------------
|
||||
|
||||
// Diccionario
|
||||
$apartamentos = [
|
||||
[
|
||||
'precio/noche' => 40,
|
||||
'ciudad' => 'Valencia',
|
||||
'wifi' => True,
|
||||
'pagina web' => 'https://hotel.com'
|
||||
],
|
||||
[
|
||||
'precio/noche' => 87,
|
||||
'ciudad' => 'Calpe',
|
||||
'wifi' => True,
|
||||
'pagina web' => 'https://calpe.com'
|
||||
],
|
||||
[
|
||||
'precio/noche' => 67,
|
||||
'ciudad' => 'Valencia',
|
||||
'wifi' => False,
|
||||
'pagina web' => 'https://denia.com'
|
||||
],
|
||||
[
|
||||
'precio/noche' => 105,
|
||||
'ciudad' => 'Benidorm',
|
||||
'wifi' => False,
|
||||
'pagina web' => 'https://benidorm.com'
|
||||
]
|
||||
];
|
||||
//array_walk (Iterar) // Puede modificar el array original
|
||||
array_walk($apartamentos, function ($apartamento, $posicion) {
|
||||
echo $posicion+1 .' '. $apartamento['ciudad'] . '<br>';
|
||||
});
|
||||
|
||||
/*
|
||||
1 Valencia
|
||||
2 Calpe
|
||||
3 Valencia
|
||||
4 Benidorm
|
||||
*/
|
||||
echo '<br>';
|
||||
//array_filter (filtrar) // No modifica el original
|
||||
$todosLosApartamentosValencia = array_filter($apartamentos, function ($apartamento) {
|
||||
return $apartamento['ciudad'] === 'Valencia';
|
||||
});
|
||||
// Extraería los dos array con ciudad Valencia
|
||||
|
||||
//array_map (modificar)
|
||||
$apartamentosMasBaratos = array_map(function ($apartamento) {
|
||||
return array_merge($apartamento, ['precio/noche' => $apartamento['precio/noche'] - 1]);
|
||||
}, $apartamentos);
|
||||
// Le restará a todos los precio/noche
|
||||
|
||||
//array_reduce (calcular)
|
||||
$media = array_reduce($apartamentos, function ($acumulador, $apartamento) {
|
||||
return $apartamento['precio/noche'] + $acumulador;
|
||||
}, 0) / count($apartamentos);
|
||||
?>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user