Académique Documents
Professionnel Documents
Culture Documents
PHP
Exécution
Exécution d’un
d’un programme
programme sur sur le
le serveur
serveur
<<html>
Construction
Construction de
de la
la réponse
réponse
html>
<<head
head>>
Serveur
Serveur
<<title
title>>Hello
Hello</
</title
title>> <?php
<?php
</
</head
head>>
Serveur Web .html
echo
echo <<<<<<HTML
HTML hello.php
hello.php
<<body
body>>Hello
Hello world
world</
</body
body>> <<html >
</ .php html>
</html
html>> <<head
head>>
.jpg
<<title
title>>Hello
Hello</
</title
title>>
Module PHP </
</head
head>>
<<body
body>>Hello
Hello world
world</</body
body>>
MySQL </
</html
html>>
HTML;
HTML;
Client Serveur
Commentaires:
/* jusqu’au prochain */
// jusqu’à la fin de la ligne
# jusqu’à la fin de la ligne
Tableau: $tab[2]=12
Ressource
NULL
$test = 12 ; // Entier
echo $test ; // 10
$total =
$nombre1 + $nombre2 + $chaine1 + $chaine2 ;
1.5
1.5 12
12 "10"
"10" 'coucou'
'coucou'
13.5
13.5 10
10
23.5
23.5 00
$nb = 6*2 ;
"Acheter " . $nb . " oeufs"
vaut "Acheter 12 oeufs"
Impossible
Impossible de
de voir
voir le
le code
code PHP
PHP depuis
depuis le
le navigateur
navigateur !!
!!
10:11:08 Programmation Web 2012-2013 15
Les opérateurs arithmétiques
$a + $b Somme
$a - $b Différence
$a * $b Multiplication
$a / $b Division
$a & $b ET binaire
$a | $b OU binaire
$a ^ $b XOR binaire
En
En cas
cas de
de doute,
doute,
utilisez
utilisez les
les parenthèses
parenthèses ;-)
;-)
while (condition)
{
/* Bloc d’instructions répété tant que la
condition est vraie */
}
do {
/* Bloc d’instructions exécuté une fois
puis répété tant que la condition est
vraie */
} while (condition) ;
Équivalent à:
avant ;
while (condition)
{ /* Bloc d’instructions répété tant que la
condition est vraie */
fin_chaque_itération ;
}
switch (val)
{
case v1:
instructions exécutées si val==v1
case v2:
instructions exécutées si val==v2
ou si val==v1
…
default:
instructions dans tous les cas
}
Création / initialisation:
$tab1=array(12, "fraise", 2.5) ;
$tab2[] = 12 ;
Clé Valeur
$tab2[] = "fraise" ;
0 12
$tab2[] = 2.5 ;
1 "fraise"
2 2.5
$tab3[0] = 12 ;
$tab3[1] = "fraise" ;
$tab3[2] = 2.5 ;
10:11:13 Programmation Web 2012-2013 28
Les tableaux « à trous »
PHP … HTML
… Val:12<br>\n
$tab4[0] = 12 ; Val:fraise<br>\n
$tab4[1] = "fraise" ; Val:2.5<br>\n
$tab4[2] = 2.5 ; Val:el5<br>\n
…
$tab4[5] = "el5" ;
foreach($tab4 as $v)
{ Navigateur
echo "Val: $v<br>\n";
}
…
$tab5['un'] = 12 ;
$tab5['trois'] = "fraise" ;
Clé Valeur
$tab5["deux"] = 2.5 ;
"un" 12
$tab5[42] = "el5" ;
"trois" "fraise"
"deux" 2.5
42 "el5"
<<html
html>>
<<head
head>>
Serveur
Serveur nomPers
<<title
title>>bonjour
bonjour</
</title
title>> nomPers
</
</head
head>>
Serveur
<<body Web <?php
<?php
body>>
Bonjour $$html
html == <<<
<<<HTML
HTML
Bonjour robert
robert !!
</ < html
<html>>
</body
body>>
</ html <<head
head><
><title
title>>bonjour
bonjour</ </title
title></></head
head>>
html>> PHP
Module
</
<<body
body>>
HTML
HTML;;
$$html
html .=.= ""Bonjour
Bonjour "".$
.$_GETrobert
robert
_GET[[''nomPers
nomPers'']].."" !!\n
\n"" ;;
echo
echo $$html
html .. ""</
</body
body>>\n
\n</
</html
html>>"" ;;
$a = 12 ;
$b = $a ; $$aa 84
12
12
84
$c = &$a ; $$bb ""coucou
12coucou""
$b = "coucou" ; $$cc ""hello
hello""
$c = 84 ;
echo
$a "\$a : $a\n"
$a :: 84
84 ;
echo
$b "\$b : $b\n"
$b :: coucou
coucou ;
echo
$c "\$c : $c\n"
$c :: 84
84
;
unset($c) ;
$c = "hello" ;
10:11:21 Programmation Web 2012-2013 51
Fonctions utilisateur
Valeur de retour
function moyenne($a,$b)
{ … }
Typage
Typage faible
faible de
de PHP
PHP ::
Aucune
Aucune information
information
Arguments
function moyenne( $a, $b)
{ … }
Typage
Typage faible
faible de
de PHP
PHP ::
Aucune
Aucune information
information
Utilisation
bonjour() ;
Bonjour
Bonjour cher
cher inconnu
inconnu
bonjour("Marcel") ;
Bonjour
Bonjour cher
cher Marcel
Marcel
10:11:23 Programmation Web 2012-2013 56
Définition de fonctions fréquemment utilisées
<?php
define("ma_constante", "Bonjour à tous") ;
nom
nom valeur
valeur
Définition
Définition d'une
d'une constante
constante
echo ma_constante ;
?>
Utilisation
Utilisation de
de la
la constante
constante
Débogage
production, E_PARSE
production, toute
toute
erreur
erreur affichée
affichée E_NOTICE
donne
donne des des indices
indices sursur E_CORE_ERROR
les
les scripts
scripts et
et rend
rend le le E_CORE_WARNIN
site
site vulnérable
vulnérable G
E_COMPILE_ERR
php.ini
php.ini OR
display_errors
display_errors boolean
boolean E_COMPILE_WAR
10:11:24
NING
Programmation Web 2012-2013 62
Opérateur de contrôle d'erreur
$v = file("dummy.txt") Fichier
Fichier absent
absent
or die("Problème de lecture") ;
Warning
Warning:: file(dummy.txt):
file(dummy.txt): failed
failed to
to open
open
stream:
stream: No
No such
such file
file or
or directory
directory in
in
dummy.php
dummy.php on
on line
line 68
68
Problème
Problème de
de lecture
lecture
$v = @file("dummy.txt")
or die("Problème de lecture") ;
Problème
Problème de
de lecture
lecture