PHP 7 > Fonctions
Utilisation des fonctionsround(), array_sum(), count(), print_r():
<?php
$notes = [10,20,13,14,16,15];
function moyenne($array){
$moy = round(array_sum($array) / count($array), 2);
print_r($moy);
}
moyenne($notes);
Utilisation des fonctionsstrtolower(), strrev(:
<?php
function is_pallindrome() {
$user_input = readline('entrez votre mot :');
$reversed_input = strtolower(strrev($user_input));
if(strtolower($user_input) === $reversed_input){
echo 'Your entry is a palindrome';
}else{
echo "This is not a palindrome";
}
}
is_pallindrome();
Utilisation des fonctionsreadline(), readline_add_history(), readline_list_history(), readline_info() :
<?php
// Lit 3 commandes de l'utilisateur
for ($i = 0; $i < 3; $i++) {
$line = readline("Commande : ");
readline_add_history($line);
}
// Liste l'historique
print_r(readline_list_history());
// Liste les variables
print_r(readline_info());
?>
Utilisation des fonctionsreadline(), str_split(), foreach():
<?php
function write_word()
{
$insultes = ["con", "batard"];
$mot = readline("Entrez un mot :");
if ($mot === $insultes[0] || $mot === $insultes[1]) {
$characters = str_split($mot);
foreach($characters as $ch) {
echo "*";
}
} else {
echo $mot;
}
}
write_word();
Utilisation des fonctionsforeach(), str_split(), str_repeat(), strlen(), substr(), readline(),str_replace():
<?php
$insultes = ["con", "batard","chier"];
$asterisque = [];
foreach ($insultes as $insulte){
$characters = str_split($insulte);
$first = $characters[0];
$asterisque[] = $first . str_repeat('*',strlen($insulte)-1);
/* Ou $asterisque[] = substr($insultes,0,1) . str_repeat('*',strlen($insulte)-1); */
}
$phrase = readline("Entrez une phrase : ");
$phrase = str_replace($insultes,$asterisque, $phrase );
echo $phrase;