Cheatography
https://cheatography.com
Syntaxe globale
Ouverture de contexte PHP |
<?php
|
Fermeture de contexte PHP |
?>
|
Commentaire (ligne simple) |
// Texte
|
Commentaire (multiligne) |
/* Texte */
|
Fin d'instruction |
;
|
Bloc d'instructions |
{ instruction; }
|
Types primitifs
bool |
int |
float |
string |
array |
object |
resource |
null |
Variables
Initialisation |
$variable = 'valeur'; $variable = $autreVariable; $variable = &$reference;
|
Test valeur vide |
empty($variable);
|
Test d'existence |
isset($variable) ;
|
Destruction |
unset($variable) ;
|
Constantes
Initialisation |
define('NOM', 'valeur');
const NOM = 'valeur';
|
Utilisation |
echo NOM;
|
Test d’existence |
defined(NOM);
|
Manipulation de booléens
Inversion |
!true
|
ET logique |
AND
&&
|
OU logique |
OR
||
|
OU exclusif logique |
XOR
|
Priorités forcées |
( test booléen )
|
|
|
Manipulation de nombres
Adition |
1 + 2
|
Soustraction |
1 - 2
|
Multiplication |
1 * 2
|
Division |
1 / 2
|
Modulo |
1 % 2
|
Incrémentation |
$nombre++
|
Décrémentation |
$nombre--
|
Augmentation |
$nombre += $valeur;
|
Diminution |
$nombre -= $valeur;
|
Manipulation de texte
Initialisation large |
"phrase"
|
Initialisation stricte |
'phrase'
|
Encapsulation |
"Phrase n°$numero !"
|
Contaténation |
'Phrase n°' . $numero . " !"
|
Recherche |
strpos()
strrpos()
|
Extraction |
substr()
|
Manipulation de tableaux
Déclaration |
$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'existence |
isset($client['nom'])
array_key_exists()
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>"; }
|
Modification d’élt |
$client['nom'] = 'B.';
|
Effacement d’élt |
unset($client['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);
}
|
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets