Enunciats dels exercicis per a començar a programar en PHP
ex01.php
</html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Començant a programar 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=\"ex01.pdf\">Enunciats dels exercicis per a començar a programar en PHP</a><HR>";
presentaCodiFont("ex01.php");
presentaCodiFont("ex01-1.php");
presentaCodiFont("ex01-2.php");
presentaCodiFont("ex01-3.php");
presentaCodiFont("ex01-4.php");
presentaCodiFont("ex01-5.php");
presentaCodiFont("ex01-6.php");
presentaCodiFont("ex01-7.php");
?>
</body>
</html>
Executa-ho
ex01-1.php
</html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Hola món!</title>
</head>
<body>
<?php
echo "Hola món!";
// Es pot fer servir un print
?>
</body>
</html>
Executa-ho
ex01-2.php
</html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Crear i utilitzar variables</title>
</head>
<body>
<?php
echo "En Pinxo li va dir a en Panxo. <br/>";
//Fixa't que les etiquetes html poden estar dins del mateix "echo"
$pinxo="Pinxo";
$panxo="Panxo";
// Això mostrarà exactament el mateix que el primer "echo"
echo "En $pinxo li va dir a en $panxo. <br/>";
$pinxo="Panxo";
$panxo="Pinxo";
// Això mostrarà els noms a l'inrevés
echo "En $pinxo li va dir a en $panxo. <br/>";
$pinxo="Manel";
$panxo="Pep";
/* Això mostrarà els nous valors
assignats a les variables
*/
echo "En $pinxo li va dir a en $panxo. <br/>";
?>
</body>
</html>
Executa-ho
ex01-3.php
</html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Operadors aritmètics</title>
</head>
<body>
<?php
$x=10;
$y=7;
$result=$x+$y;
echo "$x + $y = $result<br />";
$result=$x-$y;
echo "$x - $y = $result<br />";
$result=$x*$y;
echo "$x * $y = $result<br />";
$result=$x/$y;
echo "$x / $y = $result<br />";
$result=$x%$y;
echo "$x % $y = $result<br />";
?>
</body>
</html>
Executa-ho
ex01-4.php
</html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Assignació aritmètica d'operadors i variables</title>
</head>
<body>
<?php
$num = 8;
echo "El valor és ara $num.<br/>";
$num += 2;
echo "Afegim 2. El valor és ara $num. <br/>";
$num -= 4;
echo "Restem 4. El valor és ara $num. <br/>";
$num *= 5;
echo "Multipliquem per 5. El valor és ara $num. <br/>";
$num /= 3;
echo "Dividim per 3. El valor és ara $num. <br/>";
$num++;
echo "Incrementem el valor en un. El valor és ara $num.<br/>";
$num--;
echo "Decrementem en un. El valor és ara $num."; ?>
</body>
</html>
Executa-ho
ex01-5.php
</html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Contingut de variables i destrucció</title>
</head>
<body>
<?php
$name='Esteve';
$age=28;
var_dump($name);
echo "<br/>";
print_r($name);
echo "<br/>";
var_dump($age);
echo "<br/>";
$name=null;
// En versions de PHP anteriors a la 5.3.3, aquesta darrera
// línia es podria haver escrit com a unset($name);
// En versions més recents retorna un error de variable no definida
// per var_dump després de que la variable s'hagi desadjuntat (unset)
var_dump($name);
//Les dues darreres línies haurien funcionat també amb la variable $age
?>
</body>
</html>
Executa-ho
ex01-6.php
</html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>La concatenació de cadenes</title>
</head>
<body>
<?php
$cap = "cap";
echo 'En '.$cap.' '.$cap.' '.$cap.' el que '.$cap.' en aquest '.$cap.'.';
?>
</body>
</html>
Executa-ho
ex01-7.php
</html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Tipus de variables</title>
</head>
<body>
<?php
$queEsAixo = 'George';
echo "El tipus és ".gettype($queEsAixo).".<br/>\n";
$queEsAixo = 88.9;
echo "El tipus és ".gettype($queEsAixo).".<br/>\n";
$queEsAixo = true;
echo "El tipus és ".gettype($queEsAixo).".<br/>\n";
$queEsAixo = 8;
echo "El tipus és ".gettype($queEsAixo).".<br/>\n";
$queEsAixo = null;
echo "El tipus és ".gettype($queEsAixo).".<br/>\n";
?>
</body>
</html>
Executa-ho