Fondamentaux sur PHP Cheat Sheet

A Programming › PHP cheat sheet in français (French) by benoit

Bases du langage

18,643 views 2,949 downloads 23 May 2016 History
4.9 avg · 1 rating

Syntaxe globale

Ouverture de contexte PHP
<?php
Fermeture de contexte PHP
?>
Commen­taire (ligne simple)
// Texte
Commen­taire (multi­ligne)
/* Texte */
Fin d'in­str­uction
;
Bloc d'in­str­uctions
{ instru­ction; }

Types primitifs

bool
int
float
string
array
object
resource
null

Variables

Initia­lis­ation
$variable = 'valeur';
$variable = $autreVariable;
$variable = &$refer­ence;
Test valeur vide
empty(­$va­ria­ble);
Test d'ex­istence
isset(­$va­riable);
Destru­ction
unset(­$va­riable);

Constantes

Initia­lis­ation
define­('NO­M', '­val­eur­');
const NOM = '­val­eur­';
Utilis­ation
echo NOM;
Test d’exis­tence
define­d(NOM);

Manipu­lation de booléens

Inversion
!true
ET logique
AND
&&
OU logique
OR
||
OU exclusif logique
XOR
Priorités forcées
( test booléen )

Manipu­lation de nombres

Adition
1 + 2
Soustr­action
1 - 2
Multip­lic­ation
1 * 2
Division
1 / 2
Modulo
1 % 2
Incrém­ent­ation
$nombre++
Décrém­ent­ation
$nombre--
Augmen­tation
$nombre += $valeur;
Diminution
$nombre -= $valeur;

Manipu­lation de texte

Initia­lis­ation large
"­phr­ase­"
Initia­lis­ation stricte
'­phr­ase­'
Encaps­ulation
"­Phrase n°$numero !"
Contat­énation
'­Phrase n°'
 . $numero
 . " !"
Recherche
strpos()
strrpos()
Extraction
substr()

Manipu­lation de tableaux

Déclar­ation
$tableau = array();
$tableau = [];
Tab. numérique
auto
$nombres = [1, 9];
Tab. numérique
fixe
$nombres = [
  0 => 1,
  5 => [2, 6, 1],
];
Tab. associatif
$client = [
 '­nom­' => 'Ben',
];
Ajout auto d’élt
$nombres[] = 11;
Test d'ex­istence
isset(­$cl­ien­t['n­om'])
array_­key­_ex­ists()
in_array()
Accès au contenu
associatif
echo $client['nom'];
var_dump($client);
Accès au contenu
numérique
foreach ($nombres as $n) {
 ­ echo "­Nombre : $n <br­>";
}
Modifi­cation d’élt
$clien­t['n­om'] = '­B.';
Effacement d’élt
unset($clie­nt[­'­nom­']);

Fonctions

/**
 * @param array $chanson
 * @param int $nombreDeFois
 * @return bool True si on a pu tout chanter.
 */
function chanter($chanson, $nombreDeFois = 1)
{
    // On chante

    return true;
}

Base de données MySQL

$connexion = mysqli_connect(
  $hote,
  $compteMysql,
  $motDePasseMysql,
  $baseDeDonnees
);

if (!$connexion) {
    http_response_code(500);
    die('Erreur de connexion');
}

if ($res = mysqli_query($connexion, $sql)) {
  while ($valeurs = mysqli_fetch_assoc($res)) {
     var_dump($valeurs);
  }
  mysqli_free_result($res);
}

Badges

Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 1

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.