SlideShare une entreprise Scribd logo
1  sur  27
Télécharger pour lire hors ligne
PHP : Variables, conditions & boucles
1. Variables
Définition
A variable is a storage location paired with an associated
symbolic name (an identifier), which contains some known or
unknown quantity or information referred to as a value.The
variable name is the usual way to reference the stored value; this
separation of name and content allows the name to be used
independently of the exact information it represents.
Source : http://en.wikipedia.org
Variables & PHP
Typage automatique
Durée de vie : une exécution
Utilisation du préfixe « $ » ( ex : $maVariable )
Nommer une variable PHP (1)
Aucun espace
Aucun mots clés réservés : and, array, as, break, case, class,
clone, const, continue, default, die, do, echo, else, elseif,
empty, endif, eval, exit, extends, final, for, foreach, function,
global, goto, if, implements, include, interface, isset, list,
namespace, new, or, print, private, protected, public, return,
static, switch, trait, try, unset, use, var, while, xor …
Aucune ponctuation sauf _
Ne doit pas commencer par un chiffre
Nommer une variable (2)
// Syntax valide
$maVariable
$ma_variable
$mavariable1
// Syntax invalide
$ma variable
$ma-variable
$1mavariable
$if
Affecter une variable
<?php
$maVariable = 3;
$maSuperVariable = 'toto';
?>
Afficher une variable
<?php
$maVariable = 3;
echo $maVariable; // 3
?>
Modifier une variable
<?php
$maVariable = 3;
echo $maVariable; // 3
$maVariable = 'toto';
echo $maVariable; // toto
?>
Variable et opérations
<?php
$maVariable = 3;
$maSuperVariable = 5;
echo $maVariable + $maSuperVariable; // 8
$maMetaVariable = $maVariable * $maSuperVariable;
echo $maMetaVariable; // 15
?>
Variable et concaténation
<?php
$nom = 'Jean';
echo 'Bonjour ' . $nom; // Bonjour Jean
?>
2. structures conditionnelles
Test simple (1)
<?php
$sheldonQi = 187;
$leonardQi = 173;
if($sheldonQi > $leonardQi){
echo 'Bazinga';
}
?>
Test simple (2)
<?php
$sheldonQi = 187;
$leonardQi = 173;
if($sheldonQi > $leonardQi){
echo 'Bazinga';
}else{
echo 'My mother had me tested';
}
?>
Test simple (3)
<?php
$sheldonQi = 187;
$leonardQi = 173;
if($sheldonQi > $leonardQi){
echo 'Bazinga';
}elseif($sheldonQi == $leonardQi){
echo 'IMPOSSIBRU!!';
}else{
echo 'My mother had me tested';
}
?>
Switch
<?php
switch($myQi){
case 187:
echo "I'm Sheldon";
break;
case 173:
echo "I'm Leonard";
break;
case 164:
echo "I'm Rajesh";
break;
case 160:
echo "I'm Howard";
break;
default:
echo "I'm Penny";
break;
}
?>
3. Boucles
Boucle while (1)
La boucle while, francisée en « boucle tant que », est, en
informatique, une structure de contrôle permettant de réaliser
une boucle qui permet au code d'être exécuté de façon répétée
sur la base d'une condition booléenne donnée. La boucle while
peut être considérée comme une répétition de l'instruction if.
Source : http://fr.wikipedia.org
Boucle while (2)
<?php
$i = 0;
while ( $i < 10) {
echo $i;
$i = $i + 1;
}
// Comportement : tant que $i est inférieur à 10,
// répéter les instructions entre accolades
// Résultat : 0123456789
?>
Boucle while (3)
<?php
$i = 0;
while ( $i < 10) {
echo $i;
if($i == 4){
break;
}
$i = $i + 1;
}
// Résultat : 0123456789
?>
Boucle for (1)
La boucle for est en informatique une structure de
contrôle de programmation permettant de réaliser
une boucle associée à une variable qui sera
incrémentée à chaque itération.
Source : http://fr.wikipedia.org
Boucle for (2)
<?php
for ($i=0; $i < 10; $i++) {
echo $i;
}
// Comportement : Répéter 10 fois
// les instructions entre accolades
// Résultat : 0123456789
?>
4. Mise en pratique
Ca peut servir … (1)
<?php
// Recopie le contenu de header.php
// dans la page courante
include('header.php');
?>
Ca peut servir … (2)
<?php
// Un nombre entier entre 0 et 5
$nombreAleatoire = rand(0, 5);
echo $nombreAleatoire;
?>
Exercice : En route pour l’école !
Intégrer les écrans disponibles ici :
https://www.dropbox.com/sh/rc6scnnnsotfbai/AADVr8A1akXISfkSF7D3BCNSa?dl=0
Toute répétition de code HTML doit être gérée en PHP
(include et boucle)
La page quizz affiche une opération aléatoire
Merci pour votre attention.

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 HassaniShellmates
 
ZendFramework2 - Présentation
ZendFramework2 - PrésentationZendFramework2 - Présentation
ZendFramework2 - Présentationjulien pauli
 
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
 
Initiation au php
Initiation au phpInitiation au php
Initiation au phpStrasWeb
 
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
 
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
 
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
 
Php cours
Php coursPhp cours
Php cours
 
Cours php
Cours phpCours php
Cours php
 
ZendFramework2 - Présentation
ZendFramework2 - PrésentationZendFramework2 - Présentation
ZendFramework2 - Présentation
 
Introduction à Python
Introduction à PythonIntroduction à Python
Introduction à 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
 
Initiation au php
Initiation au phpInitiation au php
Initiation au php
 
Initiation au php
Initiation au phpInitiation au php
Initiation au php
 
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
 
Introduction au Jquery
Introduction au JqueryIntroduction au Jquery
Introduction au Jquery
 
Formation python
Formation pythonFormation python
Formation 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
 
Cours python avancé
Cours python avancéCours python avancé
Cours python avancé
 
Langage Perl
Langage PerlLangage Perl
Langage Perl
 

En vedette

Startup #7 : how to get customers
Startup #7 : how to get customersStartup #7 : how to get customers
Startup #7 : how to get customersJean Michel
 
PHP #1 : introduction
PHP #1 : introductionPHP #1 : introduction
PHP #1 : introductionJean Michel
 
WebApp #4 : Consuming REST APIs
WebApp #4 : Consuming REST APIs WebApp #4 : Consuming REST APIs
WebApp #4 : Consuming REST APIs Jean Michel
 
Javascript #6 : objets et tableaux
Javascript #6 : objets et tableauxJavascript #6 : objets et tableaux
Javascript #6 : objets et tableauxJean Michel
 
Architecture logicielle #4 : mvc
Architecture logicielle #4 : mvcArchitecture logicielle #4 : mvc
Architecture logicielle #4 : mvcJean 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
 
Gestion de projet #4 : spécification
Gestion de projet #4 : spécificationGestion de projet #4 : spécification
Gestion de projet #4 : spécificationJean Michel
 
Javascript #2.2 : jQuery
Javascript #2.2 : jQueryJavascript #2.2 : jQuery
Javascript #2.2 : jQueryJean Michel
 
PHP #3 : tableaux & formulaires
PHP #3 : tableaux & formulairesPHP #3 : tableaux & formulaires
PHP #3 : tableaux & formulairesJean Michel
 
Architecture logicielle #2 : TP timezone
Architecture logicielle #2 : TP timezoneArchitecture logicielle #2 : TP timezone
Architecture logicielle #2 : TP timezoneJean Michel
 
#3 html in the real world
#3 html in the real world#3 html in the real world
#3 html in the real worldJean Michel
 
Html & Css #6 : formulaires
Html & Css #6 : formulairesHtml & Css #6 : formulaires
Html & Css #6 : formulairesJean Michel
 
Javascript #2 : valeurs, types & opérateurs
Javascript #2 : valeurs, types & opérateursJavascript #2 : valeurs, types & opérateurs
Javascript #2 : valeurs, types & opérateursJean Michel
 
Javascript #5.1 : tp1 zombies!
Javascript #5.1 : tp1 zombies!Javascript #5.1 : tp1 zombies!
Javascript #5.1 : tp1 zombies!Jean Michel
 
Wordpress #1 : introduction
Wordpress #1 : introductionWordpress #1 : introduction
Wordpress #1 : introductionJean Michel
 
PHP #4 : sessions & cookies
PHP #4 : sessions & cookiesPHP #4 : sessions & cookies
PHP #4 : sessions & cookiesJean Michel
 
Javascript #7 : manipuler le dom
Javascript #7 : manipuler le domJavascript #7 : manipuler le dom
Javascript #7 : manipuler le domJean Michel
 
PHP #7 : guess who?
PHP #7 : guess who?PHP #7 : guess who?
PHP #7 : guess who?Jean Michel
 
Startup & entrepreneuriat #2.1: disrupt me
Startup & entrepreneuriat #2.1: disrupt meStartup & entrepreneuriat #2.1: disrupt me
Startup & entrepreneuriat #2.1: disrupt meJean Michel
 

En vedette (20)

Startup #7 : how to get customers
Startup #7 : how to get customersStartup #7 : how to get customers
Startup #7 : how to get customers
 
PHP #1 : introduction
PHP #1 : introductionPHP #1 : introduction
PHP #1 : introduction
 
WebApp #4 : Consuming REST APIs
WebApp #4 : Consuming REST APIs WebApp #4 : Consuming REST APIs
WebApp #4 : Consuming REST APIs
 
Javascript #6 : objets et tableaux
Javascript #6 : objets et tableauxJavascript #6 : objets et tableaux
Javascript #6 : objets et tableaux
 
Architecture logicielle #4 : mvc
Architecture logicielle #4 : mvcArchitecture logicielle #4 : mvc
Architecture logicielle #4 : mvc
 
#1 entreprendre au xxiè siècle
#1 entreprendre au xxiè siècle#1 entreprendre au xxiè siècle
#1 entreprendre au xxiè siècle
 
Gestion de projet #4 : spécification
Gestion de projet #4 : spécificationGestion de projet #4 : spécification
Gestion de projet #4 : spécification
 
Javascript #2.2 : jQuery
Javascript #2.2 : jQueryJavascript #2.2 : jQuery
Javascript #2.2 : jQuery
 
Projet timezone
Projet timezoneProjet timezone
Projet timezone
 
PHP #3 : tableaux & formulaires
PHP #3 : tableaux & formulairesPHP #3 : tableaux & formulaires
PHP #3 : tableaux & formulaires
 
Architecture logicielle #2 : TP timezone
Architecture logicielle #2 : TP timezoneArchitecture logicielle #2 : TP timezone
Architecture logicielle #2 : TP timezone
 
#3 html in the real world
#3 html in the real world#3 html in the real world
#3 html in the real world
 
Html & Css #6 : formulaires
Html & Css #6 : formulairesHtml & Css #6 : formulaires
Html & Css #6 : formulaires
 
Javascript #2 : valeurs, types & opérateurs
Javascript #2 : valeurs, types & opérateursJavascript #2 : valeurs, types & opérateurs
Javascript #2 : valeurs, types & opérateurs
 
Javascript #5.1 : tp1 zombies!
Javascript #5.1 : tp1 zombies!Javascript #5.1 : tp1 zombies!
Javascript #5.1 : tp1 zombies!
 
Wordpress #1 : introduction
Wordpress #1 : introductionWordpress #1 : introduction
Wordpress #1 : introduction
 
PHP #4 : sessions & cookies
PHP #4 : sessions & cookiesPHP #4 : sessions & cookies
PHP #4 : sessions & cookies
 
Javascript #7 : manipuler le dom
Javascript #7 : manipuler le domJavascript #7 : manipuler le dom
Javascript #7 : manipuler le dom
 
PHP #7 : guess who?
PHP #7 : guess who?PHP #7 : guess who?
PHP #7 : guess who?
 
Startup & entrepreneuriat #2.1: disrupt me
Startup & entrepreneuriat #2.1: disrupt meStartup & entrepreneuriat #2.1: disrupt me
Startup & entrepreneuriat #2.1: disrupt me
 

Plus de Jean 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
 
Wordpress #3 : content strategie
Wordpress #3 : content strategieWordpress #3 : content strategie
Wordpress #3 : content strategieJean 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 #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
 
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
 
Javascript #8 : événements
Javascript #8 : événementsJavascript #8 : événements
Javascript #8 : événementsJean Michel
 
WebApp #2 : responsive design
WebApp #2 : responsive designWebApp #2 : responsive design
WebApp #2 : responsive designJean Michel
 
WebApp #1 : introduction
WebApp #1 : introductionWebApp #1 : introduction
WebApp #1 : introductionJean Michel
 

Plus de Jean Michel (16)

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
 
Wordpress #3 : content strategie
Wordpress #3 : content strategieWordpress #3 : content strategie
Wordpress #3 : content strategie
 
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 #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 #6 : mysql
PHP #6 : mysqlPHP #6 : mysql
PHP #6 : mysql
 
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
 
Javascript #8 : événements
Javascript #8 : événementsJavascript #8 : événements
Javascript #8 : événements
 
WebApp #3 : API
WebApp #3 : APIWebApp #3 : API
WebApp #3 : API
 
WebApp #2 : responsive design
WebApp #2 : responsive designWebApp #2 : responsive design
WebApp #2 : responsive design
 
Projet timezone
Projet timezoneProjet timezone
Projet timezone
 
WebApp #1 : introduction
WebApp #1 : introductionWebApp #1 : introduction
WebApp #1 : introduction
 

PHP #2 : variables, conditions & boucles

  • 1. PHP : Variables, conditions & boucles
  • 3. Définition A variable is a storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity or information referred to as a value.The variable name is the usual way to reference the stored value; this separation of name and content allows the name to be used independently of the exact information it represents. Source : http://en.wikipedia.org
  • 4. Variables & PHP Typage automatique Durée de vie : une exécution Utilisation du préfixe « $ » ( ex : $maVariable )
  • 5. Nommer une variable PHP (1) Aucun espace Aucun mots clés réservés : and, array, as, break, case, class, clone, const, continue, default, die, do, echo, else, elseif, empty, endif, eval, exit, extends, final, for, foreach, function, global, goto, if, implements, include, interface, isset, list, namespace, new, or, print, private, protected, public, return, static, switch, trait, try, unset, use, var, while, xor … Aucune ponctuation sauf _ Ne doit pas commencer par un chiffre
  • 6. Nommer une variable (2) // Syntax valide $maVariable $ma_variable $mavariable1 // Syntax invalide $ma variable $ma-variable $1mavariable $if
  • 7. Affecter une variable <?php $maVariable = 3; $maSuperVariable = 'toto'; ?>
  • 8. Afficher une variable <?php $maVariable = 3; echo $maVariable; // 3 ?>
  • 9. Modifier une variable <?php $maVariable = 3; echo $maVariable; // 3 $maVariable = 'toto'; echo $maVariable; // toto ?>
  • 10. Variable et opérations <?php $maVariable = 3; $maSuperVariable = 5; echo $maVariable + $maSuperVariable; // 8 $maMetaVariable = $maVariable * $maSuperVariable; echo $maMetaVariable; // 15 ?>
  • 11. Variable et concaténation <?php $nom = 'Jean'; echo 'Bonjour ' . $nom; // Bonjour Jean ?>
  • 13. Test simple (1) <?php $sheldonQi = 187; $leonardQi = 173; if($sheldonQi > $leonardQi){ echo 'Bazinga'; } ?>
  • 14. Test simple (2) <?php $sheldonQi = 187; $leonardQi = 173; if($sheldonQi > $leonardQi){ echo 'Bazinga'; }else{ echo 'My mother had me tested'; } ?>
  • 15. Test simple (3) <?php $sheldonQi = 187; $leonardQi = 173; if($sheldonQi > $leonardQi){ echo 'Bazinga'; }elseif($sheldonQi == $leonardQi){ echo 'IMPOSSIBRU!!'; }else{ echo 'My mother had me tested'; } ?>
  • 16. Switch <?php switch($myQi){ case 187: echo "I'm Sheldon"; break; case 173: echo "I'm Leonard"; break; case 164: echo "I'm Rajesh"; break; case 160: echo "I'm Howard"; break; default: echo "I'm Penny"; break; } ?>
  • 18. Boucle while (1) La boucle while, francisée en « boucle tant que », est, en informatique, une structure de contrôle permettant de réaliser une boucle qui permet au code d'être exécuté de façon répétée sur la base d'une condition booléenne donnée. La boucle while peut être considérée comme une répétition de l'instruction if. Source : http://fr.wikipedia.org
  • 19. Boucle while (2) <?php $i = 0; while ( $i < 10) { echo $i; $i = $i + 1; } // Comportement : tant que $i est inférieur à 10, // répéter les instructions entre accolades // Résultat : 0123456789 ?>
  • 20. Boucle while (3) <?php $i = 0; while ( $i < 10) { echo $i; if($i == 4){ break; } $i = $i + 1; } // Résultat : 0123456789 ?>
  • 21. Boucle for (1) La boucle for est en informatique une structure de contrôle de programmation permettant de réaliser une boucle associée à une variable qui sera incrémentée à chaque itération. Source : http://fr.wikipedia.org
  • 22. Boucle for (2) <?php for ($i=0; $i < 10; $i++) { echo $i; } // Comportement : Répéter 10 fois // les instructions entre accolades // Résultat : 0123456789 ?>
  • 23. 4. Mise en pratique
  • 24. Ca peut servir … (1) <?php // Recopie le contenu de header.php // dans la page courante include('header.php'); ?>
  • 25. Ca peut servir … (2) <?php // Un nombre entier entre 0 et 5 $nombreAleatoire = rand(0, 5); echo $nombreAleatoire; ?>
  • 26. Exercice : En route pour l’école ! Intégrer les écrans disponibles ici : https://www.dropbox.com/sh/rc6scnnnsotfbai/AADVr8A1akXISfkSF7D3BCNSa?dl=0 Toute répétition de code HTML doit être gérée en PHP (include et boucle) La page quizz affiche une opération aléatoire
  • 27. Merci pour votre attention.