php & mysql #5 : fonctions
TRANSCRIPT
![Page 1: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/1.jpg)
PHP : Fonctions
![Page 2: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/2.jpg)
1. Une fonction ?
![Page 3: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/3.jpg)
Définition
Une fonction est un bloc de code PHP destiné généralement à être réutilisé plusieurs fois. Plutôt que d'écrire plusieurs fois le même morceau de code, on met celui-ci dans une fonction, et c'est cette fonction que l'on appellera dès que l'on en aura besoin.Source : http://oseox.fr
![Page 4: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/4.jpg)
Principe (1)
SayHello‘Jean’ ‘Bonjour Jean’
Paramètre(s) Fonction Valeur de retour
![Page 5: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/5.jpg)
Principe (2)
Addition3
8
Paramètre(s) Fonction Valeur de retour
5
![Page 6: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/6.jpg)
2. Créer une fonction
![Page 7: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/7.jpg)
Syntaxe (1)
<?php function sayHello($nom){ echo 'Bonjour '.$nom; } ?>
![Page 8: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/8.jpg)
Syntaxe (2)
<?php function addition($firstOperand, $secondOperand){ return $firstOperand + $secondOperand; } ?>
![Page 9: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/9.jpg)
Portée des variables (1)
La portée d'une variable dépend du contexte dans lequel la variable est définie. Pour la majorité des variables, la portée concerne la totalité d'un script PHP. Mais, lorsque vous définissez une fonction, la portée d'une variable définie dans cette fonction est locale à la fonction. Source : http://php.net
![Page 10: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/10.jpg)
Portée des variables (2)
<?php $thirdOperand = 3;
function addition($firstOperand, $secondOperand){ return $firstOperand + $secondOperand + $thirdOperand; }
?>
$thirdOperand existe dans et hors de la fonction.
![Page 11: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/11.jpg)
Portée des variables (3)
<?php function addition($firstOperand, $secondOperand){ $thirdOperand = 3; return $firstOperand + $secondOperand + $thirdOperand; } ?>
$thirdOperand existe uniquement dans la fonction.
![Page 12: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/12.jpg)
3. Utiliser une fonction
![Page 13: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/13.jpg)
Syntaxe (1)
<?php function sayHello($nom){ echo 'Bonjour '.$nom; }
sayHello('François'); sayHello('Nicolas'); sayHello('Jacques'); ?>
![Page 14: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/14.jpg)
Syntaxe (2)
<?php function addition($firstOperand, $secondOperand){ return $firstOperand + $secondOperand; }
echo addition(2, 4); // 6 $result = addition(1, 7); echo $result; // 8 ?>
![Page 15: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/15.jpg)
3. fonctions prédéfinies
![Page 16: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/16.jpg)
Liste des fonctions prédéfinies de PHP
http://php.net/manual/fr/funcref.php
![Page 17: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/17.jpg)
4. Mise en pratique
![Page 18: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/18.jpg)
Exercice : En route pour l’école !
Utiliser des fonctions prédéfinies pour afficher le temps que l’utilisateur a mis pour répondre au quizz.
Créer autant de fonctions que nécessaire pour simplifier votre code et éviter les répétitions.
![Page 19: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/19.jpg)
Merci pour votre attention.
![Page 20: PHP & MYSQL #5 : fonctions](https://reader031.vdocuments.pub/reader031/viewer/2022012406/55b56409bb61eb530d8b4593/html5/thumbnails/20.jpg)
CréditsPerfect Chocolate Chip Cookies - Kimberly Vardeman http://www.flickr.com/photos/kimberlykv/4643536339/