SlideShare une entreprise Scribd logo
PHP : Fonctions
1. Une fonction ?
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
Principe (1)
SayHello‘Jean’
‘Bonjour
Jean’
Paramètre(s) Fonction Valeur de retour
Principe (2)
Addition
3
8
Paramètre(s) Fonction Valeur de retour
5
2. Créer une fonction
Syntaxe (1)
<?php
function sayHello($nom){
echo 'Bonjour '.$nom;
}
?>
Syntaxe (2)
<?php
function addition($firstOperand, $secondOperand){
return $firstOperand + $secondOperand;
}
?>
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
Portée des variables (2)
<?php
$thirdOperand = 3;
function addition($firstOperand, $secondOperand){
return $firstOperand + $secondOperand + $thirdOperand;
}
?>
$thirdOperand existe dans et hors de la fonction.
Portée des variables (3)
<?php
function addition($firstOperand, $secondOperand){
$thirdOperand = 3;
return $firstOperand + $secondOperand + $thirdOperand;
}
?>
$thirdOperand existe uniquement dans la fonction.
3. Utiliser une fonction
Syntaxe (1)
<?php
function sayHello($nom){
echo 'Bonjour '.$nom;
}
sayHello('François');
sayHello('Nicolas');
sayHello('Jacques');
?>
Syntaxe (2)
<?php
function addition($firstOperand, $secondOperand){
return $firstOperand + $secondOperand;
}
echo addition(2, 4); // 6
$result = addition(1, 7);
echo $result; // 8
?>
3. fonctions prédéfinies
Liste des fonctions prédéfinies de PHP
http://php.net/manual/fr/funcref.php
4. Mise en pratique
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.
Merci pour votre attention.
Crédits
Perfect Chocolate Chip Cookies - Kimberly Vardeman
http://www.flickr.com/photos/kimberlykv/4643536339/

Contenu connexe

Tendances

Introduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El HassaniIntroduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El Hassani
Shellmates
 
Cours php
Cours phpCours php
Cours php
csskaled
 
Php cours
Php coursPhp cours
Php cours
funinformatique
 
ZendFramework2 - Présentation
ZendFramework2 - PrésentationZendFramework2 - Présentation
ZendFramework2 - Présentation
julien pauli
 
Initiation au php
Initiation au phpInitiation au php
Initiation au php
Cédric Leclinche
 
Initiation au php
Initiation au phpInitiation au php
Initiation au php
StrasWeb
 
Introduction à Python
Introduction à PythonIntroduction à Python
Introduction à Python
Abdoulaye Dieng
 
PHP 1 - Apprendre les bases
PHP 1 - Apprendre les basesPHP 1 - Apprendre les bases
PHP 1 - Apprendre les bases
Pierre Faure
 
Introduction au langage PHP (1ere partie) élaborée par Marouan OMEZZINE
Introduction au langage PHP (1ere partie) élaborée par Marouan OMEZZINEIntroduction au langage PHP (1ere partie) élaborée par Marouan OMEZZINE
Introduction au langage PHP (1ere partie) élaborée par Marouan OMEZZINE
Marouan OMEZZINE
 
Programmation shell
Programmation shellProgrammation shell
Programmation shell
Soukaina Boujadi
 
Rapport Administration des systémes Linux (Shells bash)
Rapport Administration des systémes Linux (Shells bash)Rapport Administration des systémes Linux (Shells bash)
Rapport Administration des systémes Linux (Shells bash)
Ayoub Rouzi
 
La programmation modulaire en Python
La programmation modulaire en PythonLa programmation modulaire en Python
La programmation modulaire en Python
ABDESSELAM ARROU
 
Les concepts de la programmation fonctionnelle illustrés avec Java 8
Les concepts de la programmation fonctionnelle illustrés avec Java 8Les concepts de la programmation fonctionnelle illustrés avec Java 8
Les concepts de la programmation fonctionnelle illustrés avec Java 8
Yannick Chartois
 
Introduction au Jquery
Introduction au JqueryIntroduction au Jquery
Introduction au Jquery
Abdoulaye Dieng
 
PHP #1 : introduction
PHP #1 : introductionPHP #1 : introduction
PHP #1 : introduction
Jean Michel
 
Cours python avancé
Cours python avancéCours python avancé
Cours python avancépierrepo
 

Tendances (17)

Introduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El HassaniIntroduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El Hassani
 
Cours php
Cours phpCours php
Cours php
 
Php cours
Php coursPhp cours
Php cours
 
ZendFramework2 - Présentation
ZendFramework2 - PrésentationZendFramework2 - Présentation
ZendFramework2 - Présentation
 
Initiation au php
Initiation au phpInitiation au php
Initiation au php
 
Initiation au php
Initiation au phpInitiation au php
Initiation au php
 
Introduction à Python
Introduction à PythonIntroduction à Python
Introduction à Python
 
PHP 1 - Apprendre les bases
PHP 1 - Apprendre les basesPHP 1 - Apprendre les bases
PHP 1 - Apprendre les bases
 
Introduction au langage PHP (1ere partie) élaborée par Marouan OMEZZINE
Introduction au langage PHP (1ere partie) élaborée par Marouan OMEZZINEIntroduction au langage PHP (1ere partie) élaborée par Marouan OMEZZINE
Introduction au langage PHP (1ere partie) élaborée par Marouan OMEZZINE
 
Formation python
Formation pythonFormation python
Formation python
 
Programmation shell
Programmation shellProgrammation shell
Programmation shell
 
Rapport Administration des systémes Linux (Shells bash)
Rapport Administration des systémes Linux (Shells bash)Rapport Administration des systémes Linux (Shells bash)
Rapport Administration des systémes Linux (Shells bash)
 
La programmation modulaire en Python
La programmation modulaire en PythonLa programmation modulaire en Python
La programmation modulaire en Python
 
Les concepts de la programmation fonctionnelle illustrés avec Java 8
Les concepts de la programmation fonctionnelle illustrés avec Java 8Les concepts de la programmation fonctionnelle illustrés avec Java 8
Les concepts de la programmation fonctionnelle illustrés avec Java 8
 
Introduction au Jquery
Introduction au JqueryIntroduction au Jquery
Introduction au Jquery
 
PHP #1 : introduction
PHP #1 : introductionPHP #1 : introduction
PHP #1 : introduction
 
Cours python avancé
Cours python avancéCours python avancé
Cours python avancé
 

En vedette

Html & Css #6 : formulaires
Html & Css #6 : formulairesHtml & Css #6 : formulaires
Html & Css #6 : formulaires
Jean Michel
 
#4 css 101
#4 css 101#4 css 101
#4 css 101
Jean Michel
 
Une introduction à Javascript
Une introduction à JavascriptUne introduction à Javascript
Une introduction à Javascript
Jean Michel
 
Gestion de projet #3 : besoin client
Gestion de projet #3 : besoin clientGestion de projet #3 : besoin client
Gestion de projet #3 : besoin client
Jean Michel
 
Javascript #4.1 : fonctions for noobs
Javascript #4.1 : fonctions for noobsJavascript #4.1 : fonctions for noobs
Javascript #4.1 : fonctions for noobs
Jean Michel
 
Javascript #3 : boucles & conditions
Javascript #3 : boucles & conditionsJavascript #3 : boucles & conditions
Javascript #3 : boucles & conditions
Jean Michel
 
Wordpress #3 : content strategie
Wordpress #3 : content strategieWordpress #3 : content strategie
Wordpress #3 : content strategie
Jean Michel
 
WebApp #4 : Consuming REST APIs
WebApp #4 : Consuming REST APIs WebApp #4 : Consuming REST APIs
WebApp #4 : Consuming REST APIs
Jean Michel
 
PHP #4 : sessions & cookies
PHP #4 : sessions & cookiesPHP #4 : sessions & cookies
PHP #4 : sessions & cookies
Jean Michel
 
WebApp #2 : responsive design
WebApp #2 : responsive designWebApp #2 : responsive design
WebApp #2 : responsive design
Jean Michel
 
Javascript #4.2 : fonctions for pgm
Javascript #4.2 : fonctions for pgmJavascript #4.2 : fonctions for pgm
Javascript #4.2 : fonctions for pgm
Jean Michel
 
Gestion de projet #4 : spécification
Gestion de projet #4 : spécificationGestion de projet #4 : spécification
Gestion de projet #4 : spécification
Jean Michel
 
#1 entreprendre au xxiè siècle
#1 entreprendre au xxiè siècle#1 entreprendre au xxiè siècle
#1 entreprendre au xxiè siècle
Jean Michel
 
Html & Css #5 : positionement
Html & Css #5 : positionementHtml & Css #5 : positionement
Html & Css #5 : positionement
Jean Michel
 
Javascript #2.2 : jQuery
Javascript #2.2 : jQueryJavascript #2.2 : jQuery
Javascript #2.2 : jQuery
Jean Michel
 
Wordpress #1 : introduction
Wordpress #1 : introductionWordpress #1 : introduction
Wordpress #1 : introduction
Jean Michel
 
PHP #6 : mysql
PHP #6 : mysqlPHP #6 : mysql
PHP #6 : mysql
Jean Michel
 
Architecture logicielle #4 : mvc
Architecture logicielle #4 : mvcArchitecture logicielle #4 : mvc
Architecture logicielle #4 : mvc
Jean Michel
 
Javascript #8 : événements
Javascript #8 : événementsJavascript #8 : événements
Javascript #8 : événements
Jean Michel
 

En vedette (20)

Html & Css #6 : formulaires
Html & Css #6 : formulairesHtml & Css #6 : formulaires
Html & Css #6 : formulaires
 
#4 css 101
#4 css 101#4 css 101
#4 css 101
 
Une introduction à Javascript
Une introduction à JavascriptUne introduction à Javascript
Une introduction à Javascript
 
Gestion de projet #3 : besoin client
Gestion de projet #3 : besoin clientGestion de projet #3 : besoin client
Gestion de projet #3 : besoin client
 
Javascript #4.1 : fonctions for noobs
Javascript #4.1 : fonctions for noobsJavascript #4.1 : fonctions for noobs
Javascript #4.1 : fonctions for noobs
 
Javascript #3 : boucles & conditions
Javascript #3 : boucles & conditionsJavascript #3 : boucles & conditions
Javascript #3 : boucles & conditions
 
Wordpress #3 : content strategie
Wordpress #3 : content strategieWordpress #3 : content strategie
Wordpress #3 : content strategie
 
WebApp #4 : Consuming REST APIs
WebApp #4 : Consuming REST APIs WebApp #4 : Consuming REST APIs
WebApp #4 : Consuming REST APIs
 
PHP #4 : sessions & cookies
PHP #4 : sessions & cookiesPHP #4 : sessions & cookies
PHP #4 : sessions & cookies
 
WebApp #2 : responsive design
WebApp #2 : responsive designWebApp #2 : responsive design
WebApp #2 : responsive design
 
Javascript #4.2 : fonctions for pgm
Javascript #4.2 : fonctions for pgmJavascript #4.2 : fonctions for pgm
Javascript #4.2 : fonctions for pgm
 
Gestion de projet #4 : spécification
Gestion de projet #4 : spécificationGestion de projet #4 : spécification
Gestion de projet #4 : spécification
 
#1 entreprendre au xxiè siècle
#1 entreprendre au xxiè siècle#1 entreprendre au xxiè siècle
#1 entreprendre au xxiè siècle
 
Html & Css #5 : positionement
Html & Css #5 : positionementHtml & Css #5 : positionement
Html & Css #5 : positionement
 
Projet timezone
Projet timezoneProjet timezone
Projet timezone
 
Javascript #2.2 : jQuery
Javascript #2.2 : jQueryJavascript #2.2 : jQuery
Javascript #2.2 : jQuery
 
Wordpress #1 : introduction
Wordpress #1 : introductionWordpress #1 : introduction
Wordpress #1 : introduction
 
PHP #6 : mysql
PHP #6 : mysqlPHP #6 : mysql
PHP #6 : mysql
 
Architecture logicielle #4 : mvc
Architecture logicielle #4 : mvcArchitecture logicielle #4 : mvc
Architecture logicielle #4 : mvc
 
Javascript #8 : événements
Javascript #8 : événementsJavascript #8 : événements
Javascript #8 : événements
 

Similaire à PHP & MYSQL #5 : fonctions

Initiation au langage PHP
Initiation au langage PHPInitiation au langage PHP
Initiation au langage PHP
Faouzia Benabbou
 
chapitre 4-PHP5 module web part2 (1).pdf
chapitre 4-PHP5 module web part2 (1).pdfchapitre 4-PHP5 module web part2 (1).pdf
chapitre 4-PHP5 module web part2 (1).pdf
imaneeaouattahee
 
PHP mysql Xml.doc
PHP mysql Xml.docPHP mysql Xml.doc
PHP mysql Xml.doc
GroupeExcelMarrakech
 
PHP mysql Xml.pdf
PHP mysql Xml.pdfPHP mysql Xml.pdf
PHP mysql Xml.pdf
GroupeExcelMarrakech
 
0519-pdf-php-bases-initiation.pdf
0519-pdf-php-bases-initiation.pdf0519-pdf-php-bases-initiation.pdf
0519-pdf-php-bases-initiation.pdf
RihabBENLAMINE
 
PHP Training
PHP TrainingPHP Training
PHP Training
ahmed oumezzine
 
Php4 Mysql
Php4 MysqlPhp4 Mysql
Php4 Mysql
HamdiBaklouti
 
Programmation web cours php -- sahmi academy
Programmation web   cours php -- sahmi academyProgrammation web   cours php -- sahmi academy
Programmation web cours php -- sahmi academy
Soufiane SAHMI
 
Php_Mysql.pdf
Php_Mysql.pdfPhp_Mysql.pdf
Php_Mysql.pdf
ETTAMRY
 
seance4-1 php.ppt
seance4-1 php.pptseance4-1 php.ppt
seance4-1 php.ppt
AmineReal
 
PHP 7 et Symfony 3
PHP 7 et Symfony 3PHP 7 et Symfony 3
PHP 7 et Symfony 3
Eddy RICHARD
 
Migration PHP4-PHP5
Migration PHP4-PHP5Migration PHP4-PHP5
Migration PHP4-PHP5
julien pauli
 
Php trollons mais trollons bien (Bdx.io 2015)
Php trollons mais trollons bien (Bdx.io 2015)Php trollons mais trollons bien (Bdx.io 2015)
Php trollons mais trollons bien (Bdx.io 2015)
Arnaud Langlade
 
PHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_ExtensionsPHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_Extensionsjulien pauli
 
Nouveautés php 7
Nouveautés php 7Nouveautés php 7
Nouveautés php 7
Cyrille Grandval
 

Similaire à PHP & MYSQL #5 : fonctions (20)

Formation php 1
Formation php 1Formation php 1
Formation php 1
 
Formation php 1
Formation php 1Formation php 1
Formation php 1
 
Initiation au langage PHP
Initiation au langage PHPInitiation au langage PHP
Initiation au langage PHP
 
Forma php
Forma phpForma php
Forma php
 
chapitre 4-PHP5 module web part2 (1).pdf
chapitre 4-PHP5 module web part2 (1).pdfchapitre 4-PHP5 module web part2 (1).pdf
chapitre 4-PHP5 module web part2 (1).pdf
 
PHP mysql Xml.doc
PHP mysql Xml.docPHP mysql Xml.doc
PHP mysql Xml.doc
 
PHP mysql Xml.pdf
PHP mysql Xml.pdfPHP mysql Xml.pdf
PHP mysql Xml.pdf
 
0519-pdf-php-bases-initiation.pdf
0519-pdf-php-bases-initiation.pdf0519-pdf-php-bases-initiation.pdf
0519-pdf-php-bases-initiation.pdf
 
Php seance1
Php seance1Php seance1
Php seance1
 
PHP Training
PHP TrainingPHP Training
PHP Training
 
Php4 Mysql
Php4 MysqlPhp4 Mysql
Php4 Mysql
 
Programmation web cours php -- sahmi academy
Programmation web   cours php -- sahmi academyProgrammation web   cours php -- sahmi academy
Programmation web cours php -- sahmi academy
 
Php_Mysql.pdf
Php_Mysql.pdfPhp_Mysql.pdf
Php_Mysql.pdf
 
seance4-1 php.ppt
seance4-1 php.pptseance4-1 php.ppt
seance4-1 php.ppt
 
Playing With PHP 5.3
Playing With PHP 5.3Playing With PHP 5.3
Playing With PHP 5.3
 
PHP 7 et Symfony 3
PHP 7 et Symfony 3PHP 7 et Symfony 3
PHP 7 et Symfony 3
 
Migration PHP4-PHP5
Migration PHP4-PHP5Migration PHP4-PHP5
Migration PHP4-PHP5
 
Php trollons mais trollons bien (Bdx.io 2015)
Php trollons mais trollons bien (Bdx.io 2015)Php trollons mais trollons bien (Bdx.io 2015)
Php trollons mais trollons bien (Bdx.io 2015)
 
PHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_ExtensionsPHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_Extensions
 
Nouveautés php 7
Nouveautés php 7Nouveautés php 7
Nouveautés php 7
 

Plus de Jean Michel

Startup #7 : how to get customers
Startup #7 : how to get customersStartup #7 : how to get customers
Startup #7 : how to get customers
Jean Michel
 
HTML & CSS #10 : Bootstrap
HTML & CSS #10 : BootstrapHTML & CSS #10 : Bootstrap
HTML & CSS #10 : Bootstrap
Jean Michel
 
Javascript #11: Space invader
Javascript #11: Space invaderJavascript #11: Space invader
Javascript #11: Space invader
Jean Michel
 
Javascript #10 : canvas
Javascript #10 : canvasJavascript #10 : canvas
Javascript #10 : canvas
Jean Michel
 
Architecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designArchitecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designJean Michel
 
Architecture logicielle #2 : TP timezone
Architecture logicielle #2 : TP timezoneArchitecture logicielle #2 : TP timezone
Architecture logicielle #2 : TP timezone
Jean Michel
 
Architecture logicielle #1 : introduction
Architecture logicielle #1 : introductionArchitecture logicielle #1 : introduction
Architecture logicielle #1 : introduction
Jean Michel
 
Architecture logicielle #5 : hipsto framework
Architecture logicielle #5 : hipsto frameworkArchitecture logicielle #5 : hipsto framework
Architecture logicielle #5 : hipsto framework
Jean Michel
 
Wordpress #2 : customisation
Wordpress #2 : customisationWordpress #2 : customisation
Wordpress #2 : customisation
Jean Michel
 
PHP #7 : guess who?
PHP #7 : guess who?PHP #7 : guess who?
PHP #7 : guess who?
Jean Michel
 
PHP #3 : tableaux & formulaires
PHP #3 : tableaux & formulairesPHP #3 : tableaux & formulaires
PHP #3 : tableaux & formulaires
Jean Michel
 
Dev Web 101 #2 : development for dummies
Dev Web 101 #2 : development for dummiesDev Web 101 #2 : development for dummies
Dev Web 101 #2 : development for dummies
Jean Michel
 
Startup #5 : pitch
Startup #5 : pitchStartup #5 : pitch
Startup #5 : pitch
Jean Michel
 
WebApp #3 : API
WebApp #3 : APIWebApp #3 : API
WebApp #3 : API
Jean Michel
 
Projet timezone
Projet timezoneProjet timezone
Projet timezone
Jean Michel
 
WebApp #1 : introduction
WebApp #1 : introductionWebApp #1 : introduction
WebApp #1 : introduction
Jean Michel
 
Javascript #7 : manipuler le dom
Javascript #7 : manipuler le domJavascript #7 : manipuler le dom
Javascript #7 : manipuler le dom
Jean Michel
 

Plus de Jean Michel (17)

Startup #7 : how to get customers
Startup #7 : how to get customersStartup #7 : how to get customers
Startup #7 : how to get customers
 
HTML & CSS #10 : Bootstrap
HTML & CSS #10 : BootstrapHTML & CSS #10 : Bootstrap
HTML & CSS #10 : Bootstrap
 
Javascript #11: Space invader
Javascript #11: Space invaderJavascript #11: Space invader
Javascript #11: Space invader
 
Javascript #10 : canvas
Javascript #10 : canvasJavascript #10 : canvas
Javascript #10 : canvas
 
Architecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designArchitecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented design
 
Architecture logicielle #2 : TP timezone
Architecture logicielle #2 : TP timezoneArchitecture logicielle #2 : TP timezone
Architecture logicielle #2 : TP timezone
 
Architecture logicielle #1 : introduction
Architecture logicielle #1 : introductionArchitecture logicielle #1 : introduction
Architecture logicielle #1 : introduction
 
Architecture logicielle #5 : hipsto framework
Architecture logicielle #5 : hipsto frameworkArchitecture logicielle #5 : hipsto framework
Architecture logicielle #5 : hipsto framework
 
Wordpress #2 : customisation
Wordpress #2 : customisationWordpress #2 : customisation
Wordpress #2 : customisation
 
PHP #7 : guess who?
PHP #7 : guess who?PHP #7 : guess who?
PHP #7 : guess who?
 
PHP #3 : tableaux & formulaires
PHP #3 : tableaux & formulairesPHP #3 : tableaux & formulaires
PHP #3 : tableaux & formulaires
 
Dev Web 101 #2 : development for dummies
Dev Web 101 #2 : development for dummiesDev Web 101 #2 : development for dummies
Dev Web 101 #2 : development for dummies
 
Startup #5 : pitch
Startup #5 : pitchStartup #5 : pitch
Startup #5 : pitch
 
WebApp #3 : API
WebApp #3 : APIWebApp #3 : API
WebApp #3 : API
 
Projet timezone
Projet timezoneProjet timezone
Projet timezone
 
WebApp #1 : introduction
WebApp #1 : introductionWebApp #1 : introduction
WebApp #1 : introduction
 
Javascript #7 : manipuler le dom
Javascript #7 : manipuler le domJavascript #7 : manipuler le dom
Javascript #7 : manipuler le dom
 

PHP & MYSQL #5 : fonctions