php (partie i) par mahdi ben alaya
DESCRIPTION
PARTIE 1 : Développez une application web dynamique et interactive : Comprendre le concept Web2.0. Dégager les différences entre le modèle statique et le modèle dynamique. Comprendre les balises HTML et CSS. Apprendre le langage PHP : Les Variables, les types, les structures de contrôle, les structure itérative et les fonctions. Tout ceci accompagné par des exemples et exercices.TRANSCRIPT
![Page 1: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/1.jpg)
1
Le langage PHP (partie I)
Présentation réalisée par : Mahdi BEN ALAYAElève-Ingénieur à l’ENSI & Secrétaire Général du club Junior ENSI Entreprise (2008)
Version : 22-11-2009
![Page 2: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/2.jpg)
Remerciements
• Avant de commencer, je tiens à présenter mes sincères remerciements à tous ceux qui ont contribué au bon déroulement de cette formation.
• Je tiens à remercier particulièrement M. Marouen Ommezzine et M. Wassime Abbessi qui ont assuré les formations Web2.0 et PHP pour les années 2007 et 2008 au sein du club Junior ENSI Entreprise.
2
![Page 3: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/3.jpg)
Plan•Le concept Web2.0•Le langage PHP•Pré-requis•Premiers pas•Les variables et les types•Les opérateurs arithmétiques et
logiques•Les structures de contrôles•Les structures itératives•Les fonctions•Exercices
3
![Page 4: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/4.jpg)
4
Le Concept Web2.0
![Page 5: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/5.jpg)
Introduction
5
• L’Internet est la fondation sur laquelle est construit le Web.
• Le Web est le service qui offre sur l’Internet une interface graphique permettant par simple pointage d’accéder à des documents sur le réseau.
• L’accès aux documents se fait en utilisant un logiciel de consultation (browser, aussi appelé navigateur).
![Page 6: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/6.jpg)
La page web statique
6
• Adaptée pour des sites web de petite taille, les sites vitrines, les pages au contenu permanent ou rarement mis à jour.
• N’est pas adaptée pour des sites dont les informations font l'objet de modifications fréquentes(e-business) , les sites communautaires , les blogs …
• Pour modifier une page web statique, on utilise des éditeurs html.
![Page 7: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/7.jpg)
Contenu d’une page web statique
7
• HTML : HyperText Markup Language est un langage balisé permettant de décrire la structure et la présentation des documents pour le Web .
• CSS : Cascading Style Sheets permet de partager des Feuilles de Style entre plusieurs documents HTML ou d’avoir plusieurs présentations pour un même contenu.
page.html style.cssPage web Statique
![Page 8: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/8.jpg)
Exemple d’une page web statique
8
<HTML> <HEAD>
<TITLE>titre</TITLE><link rel="stylesheet" href="style.css" type="text/css" />
</HEAD><BODY>
<h1> Hello World </h1><p> Bonjour les
Ensiens</p><img src="image.jpg"
/><br/><a href="page2.html"
>page2</a></BODY></HTML>
body{ background: green ;}h1{ color : white ;}p{ font-family: Arial ; text-align: left ; font-weight: bold ; text-decoration: none ; font-size: 13px ;}
style.csspage.html
![Page 9: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/9.jpg)
Exemple d’une page web statique
9
Notre Page web Statique
![Page 10: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/10.jpg)
La page web dynamique
10
• Construite à la demande (On the fly) par le serveur, en fonction de critères spécifiques : le nombre de visiteurs, une opération de calcul, extraire une information de la base de données, etc.
• Présentation et contenu adaptés de manière interactive, en fonction du lieu, de la date, du produit, de l’internaute, de la météo, de la langue, etc
• Plateformes et langages pour développer un contenu web dynamique : PHP, J2EE, .NET, Ruby On Rails,…
![Page 11: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/11.jpg)
Le Concept Web2.0
11
C’est quoi le Web 2.0 ? Est-ce qu’il y a des versions
du Web !!?
![Page 12: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/12.jpg)
Le Concept Web2.0
12
•Le concept Web 2.0 désigne la transition que vit actuellement le World Wide Web.
•Internet passe aujourd’hui d'une collection de sites Web à une plateforme informatique à part entière, fournissant des applications Web aux utilisateurs.
![Page 13: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/13.jpg)
Le Concept Web2.0
13
![Page 14: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/14.jpg)
Caractéristiques générales
14
• Le site ne doit pas être un jardin secret, c'est-à-dire qu'il doit être aisé de faire rentrer ou sortir des informations du système.
• Le site doit présenter des aspects de réseaux sociaux. l'utilisateur doit rester propriétaire de ses propres données.
• le site doit être entièrement utilisable à travers un navigateur standard.
![Page 15: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/15.jpg)
Le Web1.0
15
![Page 16: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/16.jpg)
Le Web2.0
16
![Page 17: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/17.jpg)
17
Evolution
du Web
![Page 18: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/18.jpg)
18
Les Outils
Web2.0
![Page 19: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/19.jpg)
19 http://fr.eyeos.org
![Page 20: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/20.jpg)
Conclusion
20
![Page 21: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/21.jpg)
Conclusion
21
![Page 22: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/22.jpg)
22
Le langage PHP
![Page 23: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/23.jpg)
Définition
23
•PHP (Hypertext Preprocessor) est un langage de scripts libre créé en 1994 par Rasmus Lerdorf qui permet de produire des pages Web dynamiques via un serveur HTTP.
![Page 24: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/24.jpg)
Caractéristiques
•Interprété : Exécution au moment de la lecture
•Open Source : Gratuit, Evolution et correction des bugs rapides par la communauté
•Extensible : Répartition en modules,…•Orienté Objets : A partir de la version 5
•Méthode RAD•Principe KISS24
Keep it Simple and Stupid ;)
Rapid Application Development
![Page 25: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/25.jpg)
25
Demande hello.html
Hello.html
1Le modèle statique
Réponse hello.html
Serveur WEBClient
![Page 26: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/26.jpg)
26
Demande
hello.php
BD
Script
Hello.php
Requête SQL
Résultat
InterprétationPage html
Statique
1Le modèle dynamique
Réponse avec
code statique
Interpréteur
Serveur d’ApplicationClient Serveur BD
![Page 27: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/27.jpg)
Pré-requis
27
![Page 28: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/28.jpg)
Pré-requis
28
•Nous avons besoin d’un • Serveur d’application : Apache, • Interpréteur : Un moteur PHP,• Serveur de base de données : MySQL
•Plusieurs personnes savent par expérience qu'il n'est pas facile d'installer un serveur Apache et que ça se complique si vous voulez y ajouter MySQL et PHP.
![Page 29: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/29.jpg)
Pré-requis
29
BD
Script
Hello.php
Interpréteur
Serveur d’applicationClient Serveur BD
FireFox
NotePad ++
Moteur PHP
MySQL
Apache
![Page 30: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/30.jpg)
Pré-requis
•Easy PHP www.easyphp.org
•WAMP www.wampserver.com
•Xampp www.apachefriends.org/fr/xampp.html
30
Heureusement des solutions « all-in-one » existent ;-)
![Page 31: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/31.jpg)
Installation de WAMP
31
![Page 32: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/32.jpg)
Utilisation de WAMP
•Télécharger et installer WAMP sur http://www.wampserver.com/
32
![Page 33: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/33.jpg)
Interface de WAMP
33
http://localhost/
![Page 34: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/34.jpg)
Premiers pas avec PHP
34
![Page 35: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/35.jpg)
Syntaxe générale du langage
35
•Il faut enregistrer notre document sous l’extension .php pour qu’il sera interprété par le serveur comme étant un document php.
<?php
//Corps du script
?>
![Page 36: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/36.jpg)
Le « Hello World »
36
<html><head>
<title> Test page </title></head><body>
<?phpecho "<h2>Hello World
:)</h2>";?>
</body></html>
![Page 37: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/37.jpg)
Les commentaires
37
• Un commentaire permet de vous y retrouver dans votre code. Ce texte est ignoré durant la génération de la page.
<?phpecho "Ceci est un test";
// commentaire /* un
commentaire */
# commentaire?>
![Page 38: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/38.jpg)
Variables et Types
38
![Page 39: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/39.jpg)
Les variables• En PHP, les variables sont représentées par
le caractère dollar "$" suivi du nom de la variable.
$Nom_de_la_variable
• Le nom est sensible à la casse ( $x != $X )Un nom de variable valide doit commencer par : une lettre ou un tiret bas (_), suivi de : lettres, chiffres ou tirets bas.
39
![Page 40: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/40.jpg)
Exemples
40
<?php
$name ;
$_name ;
$1name ;
$_1name ;
$NaMe3 ;?>
// Valide
// Valide
// Non Valide
// Valide
// Valide
![Page 41: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/41.jpg)
Les types
• PHP ne nécessite pas de déclaration explicite du type d'une variable. Le type est déterminé par le contexte d'utilisation.
• Par exemple, si vous assignez une chaîne de caractères à la variable $var , $var devient une chaîne de caractère. Si vous assignez un nombre entier à $var ,elle devient un entier.
41
![Page 42: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/42.jpg)
Les types1 – Booléens• C'est le type le plus simple. Un booléen
exprime une valeur de vérité. Il peut prendre comme valeur soit TRUE soit FALSE .
42
<?php$foo = True; // assigne la valeur TRUE à la variable $foo?>
![Page 43: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/43.jpg)
Les types2 – Entiers • Les entiers peuvent être spécifiés en base décimale
(base 10), en hexadécimale (base 16) ou octale (base 8).
• Les entiers peuvent être optionnellement précédés par le signe plus ou moins (+ ou -).
• Pour utiliser la notation octale, vous devez préfixer le nombre avec un zéro; pour utiliser la notation hexadécimale, vous devez préfixer le nombre avec 0x
43
![Page 44: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/44.jpg)
Les types
44
<?php$a = 1234; // nombre entier en
base 10
$a = -123; // nombre entier négatif
$a = 0123; // nombre entier en base 8 //(équivalent à 83 en base 10)
$a = 0x1A; //nombre entier en base 16 //(équivalent à 26 en base 10)?>
![Page 45: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/45.jpg)
Les types
3 – Les chaines de caractèreLes chaînes de caractères sont des séquences de caractères. En PHP, un caractère est un octet et il y en a 256 de possibles.
45
4 – NULLLa valeur spéciale NULL représente l'absence de valeur. Une variable avec la valeur NULL n'a pas de valeur.
![Page 46: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/46.jpg)
Exemple :
46
<?php$firstName = "Jonathan"; $lastName = "Miller";
$fullName = $firstName . " " . $lastName;
echo strtoupper($fullName);// JONATHAN MILLER
echo substr($firstName, 0, 3); //Jon?>
![Page 47: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/47.jpg)
Les types
5 - Les tableaux• Un tableau PHP est une association
ordonnée : un type qui fait correspondre des valeurs à des clés . Un tableau peut être créé avec la fonction array .
• array prend en argument des structures :key => value , séparées par des virgules. On parle alors de tableaux associatifs .
47
![Page 48: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/48.jpg)
Les types
Tableau classiqu
e
48
<?php$arr = array("foo" => "bar", 12 => true);echo $arr["foo"]; // barecho $arr[12]; // 1?>
<?php$array = array(1, 2, 3, 4, 5);echo $array[0]; // 1?>
Tableauassociatif
![Page 49: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/49.jpg)
Les types
49
<?php// Ces deux tableaux sont identiquesarray(5 => 43, 32, 56, "b" => 12);array(5 => 43, 6 => 32, 7 => 56, "b" => 12);?>
![Page 50: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/50.jpg)
Les types
50
<?php$arr = array(5 => 1, 12 => 2);$arr[] = 56; // Ceci revient à $arr[13] = 56; $arr["x"] = 42; // Ceci ajoute un nouvel élément //avec l'index "x«
unset($arr[5]); // Ceci efface un élément du //tableau
unset($arr); // Ceci efface tout le tableau?>
![Page 51: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/51.jpg)
Les conversions des types
51
<?php$a = 5.5; // $a contient 5.5 ( type float)$b = (int) 5.5; // $a contient 5 (type int)var_dump(25/7); // float(3.5714285714286)var_dump((int) (25/7)); // int(3)var_dump(round(25/7)); // float(4)
?>
var_dump() : renvoit le contenu de la variable
![Page 52: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/52.jpg)
Conversion automatique
52
<?php$a = 5 + "5" ; $a = 5 + "5abc " ; $a = 026;$a = 059638;$a = 5 + "1.5" ; ?>
// $a contient 10// $a contient 10// $a contient 22// $a contient 5// $a contient 6.5
![Page 53: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/53.jpg)
La fonction « echo »
53
<?phpecho 'Ceci est une chaîne simple';// Affiche : Ceci est une chaîne simpleecho 'Arnold a coutume de dire : "I\'ll be back" ';// Affiche : Arnold a coutume de dire "I'll be back" $nom=Arnold;echo 'Bonjour $nom';// Affiche : Bonjour, $nomecho "Bonjour $nom";// Affiche : bonjour Arnold?>
![Page 54: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/54.jpg)
Opérateurs arithmétiqueset logiques
54
![Page 55: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/55.jpg)
Opérateurs arithmétiques et logiques
• ++, -- Incrémentation/décrémentation
• ! Négation• *, /, % arithmétique• +, - arithmétique • . concaténation des chaînes• <, <=, >, >= comparaison• ==, != égalité et différence• AND ou && Et logique• OR ou || Ou logique
55
![Page 56: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/56.jpg)
Structures de contrôles
56
![Page 57: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/57.jpg)
Les structures de contrôles
1 – L’instruction IF
57
<?phpif (conditional test)
{do this;
}?>
<?phpif (conditional test){
do this;}else{
do this;}
?>
![Page 58: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/58.jpg)
Les structures de contrôles
58
<?phpif ($a > $b) { echo"a est plus grand que b"; }?><?php
if ($a > $b) { echo "a est plus grand que b";}else{ echo "a est plus petit que b";}?>
![Page 59: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/59.jpg)
Les structures de contrôles
2 – L’instruction SWITCH
59
<?phpswitch (condition variable){
case possible result1 : do this; break;
…case possible resultN : do this;
break;case default; do this;}
?>
![Page 60: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/60.jpg)
Les structures de contrôles
60
<?phpif ($i == 0) { echo "i égale 0";} elseif ($i == 1) { echo "i égale 1";} elseif ($i == 2) { echo "i égale 2";}?>
<?phpswitch ($i) { case 0: echo "i égale 0"; break; case 1: echo "i égale 1"; break; case 2: echo "i égale 2"; break;}?>
![Page 61: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/61.jpg)
Les structures de contrôles
61
Le Switch peut également fonctionner avec les chaines de caractère (contrairement à certains langages comme le langage C).
<?phpswitch ($i) {
case "tarte": echo "i est une tarte"; break;case “glace": echo "i est une glace"; break;case "gateau": echo "i est un gateau"; break;
}?>
![Page 62: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/62.jpg)
Structures itératives
62
![Page 63: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/63.jpg)
Les structures itératives1 – La boucle WHILE
63
<?phpwhile (condition is true){
do this;}
?>
![Page 64: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/64.jpg)
Les structures itératives
64
<?php$num = 11;$upperLimit = 10;$lowerLimit = 1;while ($lowerLimit <= $upperLimit){
echo "$num x $lowerLimit = " . ($num *
$lowerLimit);$lowerLimit++;
}?>
![Page 65: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/65.jpg)
Les structures itératives
2 – La boucle DO
65
<?phpdo{
do this;} while (condition is true)
?>
Avec la boucle do, on est sûr que les instructions sont exécutées au moins une fois.
![Page 66: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/66.jpg)
Les structures itératives
3 – La boucle FOR
66
<?phpfor (initialize i; condition; update
i){do this;
}?>
![Page 67: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/67.jpg)
Les structures itératives
67
<?phpfor ($x = 2; $x <= 100; $x+
+){echo "$x <br/>";
}?>
![Page 68: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/68.jpg)
Les structures itératives4 – La boucle FOREACH
68
<?phpforeach (array_expression as $value){do
this;}?><?php
foreach (array_expression as $key =>$value){
do this;}
?>
![Page 69: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/69.jpg)
Les structures itératives
69
<?php$a = array ("un" => 1,"deux" => 2,"trois" => 3,"dix-sept" => 17);foreach ($a as $k => $v) { echo "\$a[$k] => $v.\n";}?>
$arr = array(1, 2, 3, 4);foreach ($arr as $value) {
echo $value ;}
![Page 70: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/70.jpg)
Les fonctions
70
![Page 71: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/71.jpg)
Les fonctions
71
<?phpfunction nomFonction($arg_1, $arg_2, $arg_n) {
do this;return $retval;
}?>
![Page 72: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/72.jpg)
Les fonctions
72
<?php // Definition de la fonction
function getTriangleArea($base, $height){
$area = $base * $height * 0.5;return $area;
}
// Appel de la functionecho 'La surface est', getTriangleArea(10, 50);?>
![Page 73: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/73.jpg)
Les fonctions
73
<?php function affiche_bjr(){
for ($i = 0; $i < 5; $i++) echo "Bonjour! <br/>";
//Afficher 5 fois « Bonjour! » } affiche_bjr(); //Appel à la fonction?>
![Page 74: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/74.jpg)
Exercices
74
![Page 75: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/75.jpg)
Exercice
75
Créez une page web dynamique avec un contenu du jour différent de celui de la nuit. Vous pouvez utiliser la fonction php Date('H') qui donne l’heure actuelle et vous disposez des images « jour.jpg » et « nuit.jpg »
Jour Nuit
![Page 76: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/76.jpg)
Solution
76
<HTML> <HEAD>
<TITLE>Exercice</TITLE></HEAD><BODY>
<?php if ( date(‘H') < 20 ) {
echo '<h1> C’est le jour</h1><br/>
<img src= " jour.jpg" />' ;}else{
echo '<h1> C’est la nuit</h1><br/>
<img src= " jour.jpg" />' ; }
?></BODY></HTML>
![Page 77: PHP (Partie I) Par Mahdi Ben Alaya](https://reader035.vdocuments.pub/reader035/viewer/2022081504/55870278d8b42a02668b45bf/html5/thumbnails/77.jpg)
Merci pour votre attention
77