Top Banner
© Y. Viémont Introduction à PHP - MySQL Yann Viémont — 2009-2012 Université de Versailles St-Quentin
56

Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Sep 05, 2018

Download

Documents

truongdien
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

© Y. Viémont

Introduction à PHP - MySQL

Yann Viémont — 2009-2012 Université de Versailles St-Quentin

Page 2: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Plan

1.  Introduction par l'exemple

2.  Les principes de PHP

3.  Un langage de programmation

4.  Les tableaux

2

Page 3: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

© Y. Viémont

Introduction par l'exemple

Page 4: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Premier exemple

4

Page 5: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Transformation

5

Page 6: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Rendu

6

Page 7: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

PHP disparaît pour l'utilisateur

7

Page 8: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Transformation 2

8

Page 9: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Rendu 2

9

Page 10: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Mise en page - aller à la ligne

10

Page 11: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Transformation 3

11

Page 12: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Rendu 3

12

Page 13: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Mise en page - aller vraiment à la ligne

13

Page 14: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Transformation 4

14

Page 15: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Rendu 4

15

Page 16: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

© Y. Viémont

Les principes de PHP

Page 17: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

PHP : Hypertext Preprocessor

PHP est un langage de script interprété •  Un fichier PHP est interprété par le serveur •  Il produit un fichier cible HTML (ou autre) •  C'est le fichier cible qui est transmis au client

17

Page 18: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Double transformation

18

PHP" HTML" Rendu"T1" T2"

Page 19: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Partage serveur / client

19

HTML" Rendu"

PHP" HTML"T1"

T2"

Internet"

T1 : transformation sur le serveur"

T2 : rendu sur le client"

Page 20: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

PHP : langage de programmation

PHP est au départ un simple langage de script interprété

PHP s'est considérablement enrichi jusqu'à devenir : •  Un langage de programmation procédural

–  Proche de C •  Un langage de programmation orienté objet

–  Proche de Java

20

Page 21: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Exemple 2 : boucle

21

Page 22: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

La boucle s'applique à la transformation PHP → HTML

22

Page 23: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Rendu

23

Page 24: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

© Y. Viémont

Un langage de programmation

Page 25: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Possibilités du langage

PHP est un langage de programmation complet

Il permet de choisir : •  Un style procédural •  Un style orienté objet

Il donne accès à une bibliothèque standard •  Plusieurs centaines de fonctions standards •  Plusieurs milliers avec les extensions •  Couvre tous les besoins d'un site internet

25

Page 26: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Le style procédural

Avec une syntaxe proche de C : •  Types, variables, constantes •  Opérateurs, expressions •  Structures de contrôle

–  Tests, aiguillages –  Boucles, itérations –  Débranchements

•  Fonctions •  Références •  Variables d'environnement prédéfinies

26

Page 27: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Le style orienté objet

Avec une syntaxe proche de Java / C++ : •  Objets, construction, destruction •  Références •  Interfaces •  Classes, héritage, méthodes •  Classes et méthodes abstraites •  Propriétés dynamiques •  Exceptions

27

Page 28: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Documentation PHP

Manuel PHP complet – version française

http://php.net/manual/fr/

Référence du langage (chapitre)

http://php.net/manual/fr/langref.php

Extension mysqli (Extension sur les bases de données)

http://php.net/manual/fr/book.mysqli.php

28

Page 29: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Sources de cours sur internet Cours de PHP 5 - Guillaume Rossolini

http://g-rossolini.developpez.com/tutoriels/php/cours/

Introduction au langage PHP – « Emacs » (pseudonyme)

http://www.apprendre-php.com/tutoriels/tutoriel-19-introduction-au-langage-php.html

Le PHP Facile - Anomyme

http://www.lephpfacile.com/cours

Concevez votre site web avec PHP et MySQL - Mathieu Nebra

http://www.siteduzero.com/tutoriel-3-14668-concevez-votre-site-web-avec-php-et-mysql.html

PHP : Le tutoriel pour grands débutants pressés - Sylvie Vauthier

http://sylvie-vauthier.developpez.com/tutoriels/php/grand-debutant/

29

Page 30: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Commentaires

30

Page 31: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Variables

31

Page 32: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Référence (indirection)

32

Page 33: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Variables prédéfinies

Il y a de nombreuses variables prédéfinies pour accéder aux propriétés du serveur.

Exemple : récupération des valeurs d’un formulaire

33

Page 34: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Types

4 types scalaires : •  boolean •  integer •  float •  string

2 types composés : •  array •  object

34

Page 35: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Exemples

35

Page 36: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Opérateurs

Principales catégories : •  Opérateurs arithmétiques •  Opérateurs d'assignation •  Opérateurs d'incrémentation et décrémentation •  Opérateurs sur les bits •  Opérateurs de comparaison •  Opérateurs logiques •  Opérateurs de chaînes •  Opérateurs de tableaux

36

Page 37: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Opérateurs arithmétiques

37

Page 38: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Incrémentation - Décrémentation

38

Page 39: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Comparaisons - Logique

Comparaisons des valeurs :

== < <= > >= !=

Comparaisons des types :

=== !==

Logique :

&& || ! and or xor

39

Page 40: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Concaténation

40

Page 41: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Les objets

On peut utiliser les objets en style procédural comme des structures C

41

Page 42: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Création - Accès

Création :

Accès :

$person->age = 23 ;!

echo 'Age = ' . $person->age ;!

42

Page 43: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Structures de contrôle

Tests et aiguillages :

if else elseif switch

Boucles :

while do-while for foreach

Branchements :

break continue return (goto)

43

Page 44: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Tests

44

Page 45: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Aiguillage

45

Page 46: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Boucles

46

Page 47: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Fonctions

Déclaration / Appel / Retour

47

Page 48: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Passage de paramètres

Mode par défaut à par valeur

On peut cependant faire

48

Page 49: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Syntaxe PHP / HTML

On peut réaliser : •  Un document HTML qui contient un morceau de

code PHP •  Un document PHP (code) qui génère du HTML en

impression •  Un document mixte qui entremêle les deux

syntaxes •  Un code PHP sans rapport avec HTML

49

Page 50: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

PHP inclut dans HTML

50

Page 51: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

PHP presque "pur"

51

Page 52: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Deux syntaxes entremêlées

52

Page 53: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

© Y. Viémont

Les tableaux

Page 54: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Tableaux dynamiques et associatifs

PHP permet de créer des tableaux dynamiquement : •  Grâce à la fonction array ()

Les tableaux : •  Sont indicé par défaut à partir de zéro •  Peuvent être composés de couples

(clé ==> valeur) •  Les clés peuvent être des chaînes de caractères

ou des entiers •  Les indices sont traités comme des clés

entières

54

Page 55: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Tableau indicé classique

$tableau = array ($val1, $val2, $val3, ...) ;

Déclaration et création : $tab1 = array (1, 3, 7, 12, 18) ; $tab2 = array ("IN111", 2009, "Cours PHP") ;

Usage : if ($tab2[2] == "Cours PHP") echo "C'est le bon cours" ; $taille = sizeof ($tab1) ; for ($i = 0 ; $i < $taille ; $i++) echo $tab1[$i]."<br>" ;

55

Page 56: Introduction à PHP-MySQL - e-campus 2e-campus2.uvsq.fr/Members/iulisand/Fichiers/5.-php-mysql.pdf · Introduction à PHP - MySQL Présentation de PHP Plan 1. Introduction par l'exemple

Présentation de PHP Introduction à PHP - MySQL

Boucle foreach

56