Fonction carré
Ecrire une fonction carre qui calculera le carré du
paramètre passé en argument.
Carré :
<?php if (isset($_POST['valeur'])) echo carre($_POST['valeur']); ?>
Factorielle (récursive)
Ecrire une fonction factorielle permettant de calculer la
factorielle du paramètre passé en argument, de façon récursive.
Une focntion récursive est une fonction qui s'appelle elle-même.
Indice :
Factorielle :
Une focntion récursive est une fonction qui s'appelle elle-même.
Indice :
n! = n * (n-1)!
<?php function factorielle($arg) { if ($arg < 1) return 'Erreur'; if ($arg == 1) return 1; return $arg * factorielle($arg - 1); } ?>
<?php if (isset($_POST['inc'])) { echo factorielle($_POST['fac']); } ?>
Liste aléatoire (récursive)
Ecrire une fonction randomList permettant de générer une liste aléatoire.
Pour obtenir une valeur aléatoire, vous utiliserez la fonction
$current Liste\n$children \n";
} else $item .= "$current Liste remplacée \n";
} else {
// Case item
$item .= "$current Element \n";
}
}
// We prevent empty ul
if ($item != "")
$str = "
Pour obtenir une valeur aléatoire, vous utiliserez la fonction
rand(int min, int max)
- 1 Liste
- 1.1 Liste
- 1.1.1 Element
- 1.1.2 Liste
- 1.1.2.1 Liste
- 1.1.2.1.1 Element
- 1.1.2.1.2 Element
- 1.1.2.1.3 Element
- 1.1.2.1.4 Element
- 1.1.2.1 Liste
- 1.2 Liste
- 1.2.1 Liste
- 1.2.1.1 Liste remplacée
- 1.2.1.2 Liste
- 1.2.1.2.1 Element
- 1.2.1.2.2 Element
- 1.2.1.2.3 Liste
- 1.2.1.2.3.1 Liste remplacée
- 1.2.1.3 Liste
- 1.2.1.3.1 Liste
- 1.2.1.3.1.1 Liste remplacée
- 1.2.1.3.1.2 Liste remplacée
- 1.2.1.3.1.3 Element
- 1.2.1.3.2 Liste
- 1.2.1.3.2.1 Element
- 1.2.1.3.2.2 Element
- 1.2.1.3.3 Element
- 1.2.1.3.4 Element
- 1.2.1.3.1 Liste
- 1.2.1.4 Liste
- 1.2.1.4.1 Element
- 1.2.1.4.2 Element
- 1.2.1.4.3 Liste
- 1.2.1.4.3.1 Element
- 1.2.1.4.3.2 Liste remplacée
- 1.2.1.4.3.3 Element
- 1.2.1.4.4 Element
- 1.2.1 Liste
- 1.1 Liste
<?php echo (randomList("")); /** * Generates a random HTML list * @param $parent index of parent * @return string HTML content of the list */ function randomList($parent) { $min = 0; if ($parent == "") $min = 1; $max = rand($min, 5); // We limit the depth of the tree if (strlen($parent) > 10) return null; if ($max > 0) { $item = ""; for ($i = 1; $i <= $max; $i++) { // Creation of current index $current = "$i"; if ($parent != "") { $current = "$parent.$i"; } // Is the next item a leave or a list ? if (rand(0, 1)) { // Case list $children = randomList($current); if ($children != null) { $item .= "
- $item