Enunciats dels exercicis de funcions en PHP
ex05.php
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Funcions en PHP</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=\"ex05.pdf\">Enunciats dels exercicis de funcions en PHP</a><HR>";
presentaCodiFont("ex05.php");
presentaCodiFont("ex05-1.php");
presentaCodiFont("ex05-2.php");
presentaCodiFont("ex05-2-2.php");
presentaCodiFont("ex05-3.php");
presentaCodiFont("ex05-4.php");
presentaCodiFont("ex05-5-1.php");
presentaCodiFont("ex05-5-2.php");
presentaCodiFont("ex05-6.php");
?>
</body>
</html>
Executa-ho
ex05-1.php
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Funció hola</title>
</head>
<body>
<h2>Funció hola</h2>
<?php
//Definició de la funció
function hola(){
echo "Hola món!";
}
//Crida a la funció
hola();
?>
</body>
</html>
Executa-ho
ex05-2.php
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Funció de l'àrea del rectangle</title>
</head>
<body>
<h2>Funció de l'àrea del rectangle</h2>
<?php
//Definició de la funció
function recArea($l, $w){
$area = $l * $w;
echo "Un rectangle de longitud $l i amplada $w té un àrea de $area.";
}
//Crida a la funció
recArea(2, 4);
?>
</body>
</html>
Executa-ho
ex05-2-2.php
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Funció de l'àrea del rectangle</title>
</head>
<body>
<h2>Funció de l'àrea del rectangle</h2>
<?php
//Definició de la funció
function recArea($l, $w){
$area = $l * $w;
return $area;
}
//Crida a la funció
echo "Un rectangle de longitud 2 i amplada 4 té un àrea de " . recArea(2,4) .".";
?>
</body>
</html>
Executa-ho
ex05-3.php
<?php
//Definició de la funció
function recArea($l, $w){
$area = $l * $w;
return $area;
}
?>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Funció de l'àrea del rectangle</title>
</head>
<body>
<h2>Funció de l'àrea del rectangle</h2>
<?php
// Si no s'ha premut el botó, es va al formulari inicial
if(!isset ($_POST['submit'])){
?>
<form method="post" action="ex05-3.php">
<p>Si us plau, entra els valors de la base i l'amplada d'un rectangle.</p>
<p>Base: <input type="text" name="base" size="5" />
Amplada: <input type="text" name="amplada" size="5" /></p>
<input type="submit" name="submit" value="Ves" />
</form>
<?php
// Si s'ha premut el botó es processa l'entrada
} else {
//Obtè els valors
$b = $_POST['base'];
$a = $_POST['amplada'];
$area = recArea($b, $a);
//Empra la funció amb els valors d'usuari interoduïts
//echo "Un rectangle de base $b i alçada $a té un àrea de " . recArea($b, $a). ".<BR>";
echo "Un rectangle de base $b i alçada $a té un àrea de $area.";
}
?>
</body>
</html>
Executa-ho
ex05-4.php
<?php
//Creació de la matriu
$mesos=array(
'gener'=>31,
'febrer'=>'28 dies, en any de traspàs 29',
'març'=>31,
'abril'=>30,
'maig'=>31,
'juny'=>30,
'juliol'=>31,
'agost'=>31,
'setembre'=>30,
'octubre'=>31,
'novembre'=>30,
'desembre'=>31
);
//Definició de la funció.
//La funció ucfirst posa la primera lletra en majúscula
function opcio($szMes){
echo "<option value=\"$szMes\">" .ucfirst($szMes). "</option>\n";
}
?>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Funcions per HTML</title>
</head>
<body>
<h2>Dies en un mes</h2>
<?php
if(!isset ($_POST['submit'])){
?>
<form method="post" action="ex05-4.php">
<p>Si us plau, escolliu un mes</p>
<select name="mes">
<?php
//Crea opcions emprant la matriu de la funció
foreach ($mesos as $k => $v){
opcio($k);
}
?>
</select>
<p />
<input type="submit" name="submit" value="Ves" />
</form>
<?php
} else {
$mes = $_POST['mes'];
if ($mes == 'febrer'){
echo "El mes de febrer té " . $mesos['febrer'] . ".";
}else{
echo "El mes de $mes té $mesos[$mes] dies.";
}
}
?>
</body>
</html>
Executa-ho
ex05-5-1.php
<?php
function botSel(){
$args=func_get_args();
foreach ($args as $a){
echo "<input type=\"checkbox\" name=\"tempsMeteo[]\" value=\"$a\" />" .
ucwords($a). "<br />\n";
}
}
?>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Matrius des de l'entrada d'usuari</title>
</head>
<body>
<h2>Quina percepció teniu del temps metereològic? </h2>
<?php
// Si no s'ha premut el botó, es va al formulari inicial
if (!isset($_POST['submit'])){
?>
<form method="post" action="ex05-5-1.php">
<p>Introdueixi la seva informació:</p>
Ciutat: <input type="text" name="ciutat" />
Mes: <input type="text" name="mes" />
Any: <input type="text" name="any" />
<p>Si us plau, seleccioni quin tipus de temps metereològic heu experimentat.
<br />Escolliu el que toqui. </p>
<?php
botSel('assolellat', 'núvols', 'pluja', 'calamarsa', 'aiguaneu', 'neu', 'vent',
'fred', 'calor');
?>
<input type="submit" name="submit" value="Endavant" />
</form>
<?php
// Si s'ha premut el botó es processa l'entrada
}else{
//Recull la informació de la data i el lloc
$entradaLocal = array(
$_POST['ciutat'],
$_POST['mes'],
$_POST['any']
);
echo "A $entradaLocal[0] al mes de/d' $entradaLocal[1] de $entradaLocal[2], heu observat aquest temps:<br /> <ul>";
foreach($_POST['tempsMeteo'] as $w){
echo "<li>".ucwords($w)."</li>\n";
}
echo "</ul>";
}
?>
</body>
</html>
Executa-ho
ex05-5-2.php
<?php
function botSel(){
$args=func_get_args();
foreach ($args as $a){
echo "<input type=\"checkbox\" name=\"tempsMeteo[]\" value=\"$a\" />" .
ucwords($a). "<br />\n";
}
}
function llistaHo($args){
if (!is_array($args)){
$args = explode(',', $args);
}
foreach ($args as $a){
echo "<li>" . ucwords($a). "</li>\n";
}
}
?>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Matrius des de l'entrada d'usuari</title>
</head>
<body>
<h2>Quina percepció teniu del temps metereològic? </h2>
<?php
// Si no s'ha premut el botó, es va al formulari inicial
if (!isset($_POST['submit'])){
?>
<form method="post" action="ex05-5-2.php">
<p>Introdueixi la seva informació:</p>
Ciutat: <input type="text" name="ciutat" />
Mes: <input type="text" name="mes" />
Any: <input type="text" name="any" />
<p>Si us plau, seleccioni quin tipus de temps metereològic heu experimentat.
<br />Escolliu el que toqui. </p>
<?php
botSel('assolellat', 'núvols', 'pluja', 'calamarsa', 'aiguaneu', 'neu', 'vent',
'fred', 'calor', 'humitat', 'boira');
?>
<!--Nou requeriment de dades a l usuari-->
<p>Alguna cosa més? Si us plau introdueix les noves condicions metereològiques,
separades per comes.</p>
<input type="text" name="mes" size="60" /><p />
<input type="submit" name="submit" value="Endavant" />
</form>
<?php
// Si s'ha premut el botó es processa l'entrada
}else{
//Recull la informació de la data i el lloc
$entradaLocal = array(
$_POST['ciutat'],
$_POST['mes'],
$_POST['any']
);
echo "A $entradaLocal[0] al mes de/d' $entradaLocal[1] de $entradaLocal[2], heu observat aquest temps:<br /> <ul>";
$tempsMeteo = $_POST['tempsMeteo'];
//foreach($_POST['tempsMeteo'] as $w){
// echo "<li>".ucwords($w)."</li>\n";
//}
$mes = $_POST['mes'];
llistaHo($tempsMeteo);
llistaHo($mes);
echo "</ul>";
}
?>
</body>
</html>
Executa-ho
ex05-6.php
<?php
//Creació de la matriu
$mesos=array(
'gener'=>31,
'febrer'=>'28 dies, en any de traspàs 29',
'març'=>31,
'abril'=>30,
'maig'=>31,
'juny'=>30,
'juliol'=>31,
'agost'=>31,
'setembre'=>30,
'octubre'=>31,
'novembre'=>30,
'desembre'=>31
);
//Definició de la funció.
//La funció ucfirst posa la primera lletra en majúscula
function opcio($szMes){
echo "<option value=\"$szMes\">" .ucfirst($szMes). "</option>\n";
}
function fesOpcions($matriu){
//Crea opcions emprant la matriu de la funció
foreach ($matriu as $k => $v){
opcio($k);
}
}
function fesQuadreCombinat($nom,$matriu){
if (!is_array($matriu)){
exit ("Error: no és una matriu.");
//Es podia haver escrit: die ("Error: no és una matriu.");
}
//Comença l'HTML pel camp seleccionat
echo "<select name=\"$nom\">\n";
fesOpcions($matriu);
echo "</select>" ;
}
?>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Funcions per HTML</title>
</head>
<body>
<h2>Dies en un mes</h2>
<?php
if(!isset ($_POST['submit'])){
?>
<form method="post" action="ex05-6.php">
<p>Si us plau, escolliu un mes</p>
<?php
fesQuadreCombinat("mes",$mesos);
?>
<p />
<input type="submit" name="submit" value="Ves" />
</form>
<?php
} else {
$mes = $_POST['mes'];
if ($mes == 'febrer'){
echo "El mes de febrer té " . $mesos['febrer'] . ".";
}else{
echo "El mes de $mes té $mesos[$mes] dies.";
}
}
?>
</body>
</html>
Executa-ho