Professional Documents
Culture Documents
Tableaux (cont)
Code PHP
$tableau["cle"] = "valeur";
// Commentaire
Cration
$tableau = array('valeur1',
numrique
'valeur2');
Fonction utilisateur
Cration
associative
'valeur2');
echo $tableau[4];
ligne
Commentaires sur
/* Commentaire */
plusieurs lignes
Fin d'instruction
Inclusion de fichier
require_once('nom_fic
hier.php');
Ecriture depuis
numrique
Ecriture depuis
Variables et Constantes
$nomVariable = "Chaine de caractere";
$nomVariable = 'Chaine de caractere';
echo $tableau['cle1'];
association
Tableaux
numriques
Tabulation
$param1 = 4;
$param2 = 8;
$resultat = multiplier($param1, $param2);
Classes and Objects
class SomeClass {
Tableaux
associatifs
de caractres
Matrice (tableau
$matrice[2][3] = "valeur";
echo $nomVariable;
multi-dimensions
global $varGlobale;
Types
$nomVariable = 5;
echo $GLOBALS['varGlobale']
define('NOMCONSTANTE', 'valeur');
echo NOMCONSTANTE;
isset($var);
l'existence de la
Dtruire une
unset($var);
variable
Connatre le type
gettype($var);
Vrifier un type
is_[type]($var); Ex :
is_string($var);
Cast
(changement de
type)
Conversion de
[type]val($var); Ex :
valeur
intval($var);
$floatval($var);
Test si la variable
empty($var);
est vide
Tableaux
Cration
$tableau = array();
Ajout
$tableau[] = "valeur";
$tableau[4] = "valeur";
public $anotherProperty;
protected $yetAnotherProperty = null;
$this->property = $arg;
Boolen
boolean
true OU false
Entier
integer
nombre positif ou
Nombre
float
flottant
Nombre
variable
private $property;
\t
double
Chane de
nombre virgule
positif ou ngatif
chane de
caractre
echo Hi;
}
positif uniquement
string
ngatif
nombre virgule
flottant
caractres
"chaine de caractres"
guillement ou
ou 'chaine de
apostrophe
caractres'
$var = caractres;
variables sont
return $this->property;
}
public function setProperty( $p )
{
$this->property = $p;
}
}
$myObject = new SomeClass( 123 );
echo $myObject->getProperty(); // 123
$myObject->property; // ERROR:private
interprts
Caractre
d'chappement
Retour la ligne
\n
Retour chariot
\r
By Zetura (Zetura)
Sponsored by Readability-Score.com
cheatography.com/zetura/
atago.fr
Page 1 of 3.
https://readability-score.com
Operateurs logiques
Affectation
Affectation par
$var = 5;
&=
&= $var;
+
ET
&&
Un seul
$var = $var *
$var = $var /
5;
Modulo
Incrmentation
$var = $var %
++
Dbut de chane
\d
Chiffre entre 0 et 9
\w
Retourne true si 2
Retourne true si une
true
OU
||
ET non
AND
prioritaire
Un seul non
5;
prioritaire
$var =
OU non
XOR
OR
Classe de caractres
{x} {x,}
prioritaire
{x,y}
Entre x et y
Quantit de 0 ou plus
prioritaire
Quantit de 0 ou 1
Quantit de 1 ou plus
OU
Caractre d'chappement
Oprateurs
[oprate
$var += 5;
combins
ur]=
$var *= 5;
Concatnation
echo $var."
substr($string, start,
chaine";
d'une chane
length);
$var .= "
Transformer une
explode(',', $string);
chaine";
chaine en tableau
Egalit
if($var ==
5)
Infrieur
<
if($var < 5)
Infrieur ou gal
<=
if($var <=
5)
Suprieur
>
if($var > 5)
Suprieur ou gal
>=
if($var >=
5)
Diffrent de
!=
if($var !=
5)
===
type)
if($var ===
5)
Diffrent en valeur ou en
!==
type
implode(',', $tableau);
tableau en chaine
==
espace
[]
prioritaire
Operateurs de comparaison
moins prioritaire
$var++;
Concatner un
Groupe
$var = $var--;
assignation
Espace
()
--
.=
\s
Fin de chane
Dcrmentation
Concatnation et
9A-Za-z]
condition true
5;
DIvision
inversement
$var = $var 5;
Multiplication
conditions true
$var = $var +
5;
Soustraction
$nouvelVar
rfrence
Addition
Inversion
Expression reguliere
trim($string);
au dbut et la fin
if (condition) {
// Instructions
}
elseif (condition) {
d'une chane
// Instructions
Remplacer par b
str_replace('a', 'b',
$string);
else {
Vrifier une
preg_match('regex',
expression rgulire
$string);
Remplacer une
preg_replace('regex',
expression rgulire
'b', $string);
par b
// Instructions
}
if( $something == true ) {
// Si $something vaut true
doSomething();
} elseif ( $something == false ) {
exit();
Envoyer un mail
mail($mailDest, $sujet,
doSomethingElse();
$message, 'From:
if($var !==
'.$mailEnvoi);
5)
} else {
// sinon, excuter doNothing();
doNothing();
}
if(condition):
By Zetura (Zetura)
Sponsored by Readability-Score.com
cheatography.com/zetura/
atago.fr
Page 2 of 3.
https://readability-score.com
Boucle FOR
CONTINUE
// Instructions
endif;
if ($i == 2)
continue;
false;
// Instructions
switch ($var) {
case 1:
// Instructions
// Instructions
break;
case "test":
Boucle FOREACH
// Instructions
break;
default:
// Instructions
break;
lment.
foreach($tableau as $element){
// Instructions
}
foreach($tableau as $key => $value){
// Instructions
}
$tableau = array(
'prenom' => 'Obi-wan',
'nom' => 'Kenobi',
'metier' => 'Jedi'
);
foreach($tableau as $contenu){
$i = 1;
echo $i;
$i++;
}
Excution au moins une premire fois
$i = 1;
}
Attention, la boucle fonctionne sur une copie du
do{
echo $i;
mme
$i++;
}
while($i < 10);
Attention aux boucles infinies
By Zetura (Zetura)
Sponsored by Readability-Score.com
cheatography.com/zetura/
atago.fr
Page 3 of 3.
https://readability-score.com