SlideShare a Scribd company logo
1 of 20
Download to read offline
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/

More Related Content

What's hot

Introduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El HassaniIntroduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El HassaniShellmates
 
ZendFramework2 - Présentation
ZendFramework2 - PrésentationZendFramework2 - Présentation
ZendFramework2 - Présentationjulien pauli
 
Initiation au php
Initiation au phpInitiation au php
Initiation au phpStrasWeb
 
PHP 1 - Apprendre les bases
PHP 1 - Apprendre les basesPHP 1 - Apprendre les bases
PHP 1 - Apprendre les basesPierre 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 OMEZZINEMarouan OMEZZINE
 
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 PythonABDESSELAM 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 8Yannick Chartois
 
PHP #1 : introduction
PHP #1 : introductionPHP #1 : introduction
PHP #1 : introductionJean Michel
 
Cours python avancé
Cours python avancéCours python avancé
Cours python avancépierrepo
 

What's hot (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é
 

Viewers also liked

Html & Css #6 : formulaires
Html & Css #6 : formulairesHtml & Css #6 : formulaires
Html & Css #6 : formulairesJean Michel
 
Une introduction à Javascript
Une introduction à JavascriptUne introduction à Javascript
Une introduction à JavascriptJean Michel
 
Gestion de projet #3 : besoin client
Gestion de projet #3 : besoin clientGestion de projet #3 : besoin client
Gestion de projet #3 : besoin clientJean Michel
 
Javascript #4.1 : fonctions for noobs
Javascript #4.1 : fonctions for noobsJavascript #4.1 : fonctions for noobs
Javascript #4.1 : fonctions for noobsJean Michel
 
Javascript #3 : boucles & conditions
Javascript #3 : boucles & conditionsJavascript #3 : boucles & conditions
Javascript #3 : boucles & conditionsJean Michel
 
Wordpress #3 : content strategie
Wordpress #3 : content strategieWordpress #3 : content strategie
Wordpress #3 : content strategieJean 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 & cookiesJean Michel
 
WebApp #2 : responsive design
WebApp #2 : responsive designWebApp #2 : responsive design
WebApp #2 : responsive designJean Michel
 
Javascript #4.2 : fonctions for pgm
Javascript #4.2 : fonctions for pgmJavascript #4.2 : fonctions for pgm
Javascript #4.2 : fonctions for pgmJean Michel
 
Gestion de projet #4 : spécification
Gestion de projet #4 : spécificationGestion de projet #4 : spécification
Gestion de projet #4 : spécificationJean Michel
 
#1 entreprendre au xxiè siècle
#1 entreprendre au xxiè siècle#1 entreprendre au xxiè siècle
#1 entreprendre au xxiè siècleJean Michel
 
Html & Css #5 : positionement
Html & Css #5 : positionementHtml & Css #5 : positionement
Html & Css #5 : positionementJean Michel
 
Javascript #2.2 : jQuery
Javascript #2.2 : jQueryJavascript #2.2 : jQuery
Javascript #2.2 : jQueryJean Michel
 
Wordpress #1 : introduction
Wordpress #1 : introductionWordpress #1 : introduction
Wordpress #1 : introductionJean Michel
 
Architecture logicielle #4 : mvc
Architecture logicielle #4 : mvcArchitecture logicielle #4 : mvc
Architecture logicielle #4 : mvcJean Michel
 
Javascript #8 : événements
Javascript #8 : événementsJavascript #8 : événements
Javascript #8 : événementsJean Michel
 

Viewers also liked (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
 

Similar to PHP & MYSQL #5 : fonctions

Similar to 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
 
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
 
Les principes de base de PHP
 Les principes de base de PHP  Les principes de base de PHP
Les principes de base de PHP
 

More from Jean Michel

Startup #7 : how to get customers
Startup #7 : how to get customersStartup #7 : how to get customers
Startup #7 : how to get customersJean Michel
 
HTML & CSS #10 : Bootstrap
HTML & CSS #10 : BootstrapHTML & CSS #10 : Bootstrap
HTML & CSS #10 : BootstrapJean Michel
 
Javascript #11: Space invader
Javascript #11: Space invaderJavascript #11: Space invader
Javascript #11: Space invaderJean Michel
 
Javascript #10 : canvas
Javascript #10 : canvasJavascript #10 : canvas
Javascript #10 : canvasJean 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 timezoneJean Michel
 
Architecture logicielle #1 : introduction
Architecture logicielle #1 : introductionArchitecture logicielle #1 : introduction
Architecture logicielle #1 : introductionJean Michel
 
Architecture logicielle #5 : hipsto framework
Architecture logicielle #5 : hipsto frameworkArchitecture logicielle #5 : hipsto framework
Architecture logicielle #5 : hipsto frameworkJean Michel
 
Wordpress #2 : customisation
Wordpress #2 : customisationWordpress #2 : customisation
Wordpress #2 : customisationJean 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 & formulairesJean 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 dummiesJean Michel
 
Startup #5 : pitch
Startup #5 : pitchStartup #5 : pitch
Startup #5 : pitchJean Michel
 
WebApp #1 : introduction
WebApp #1 : introductionWebApp #1 : introduction
WebApp #1 : introductionJean Michel
 
Javascript #7 : manipuler le dom
Javascript #7 : manipuler le domJavascript #7 : manipuler le dom
Javascript #7 : manipuler le domJean Michel
 

More from 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