web:php:chap1

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
web:php:chap1 [2023/11/08 15:22] – supprimée - modification externe (Unknown date) 127.0.0.1web:php:chap1 [2023/11/08 15:22] (Version actuelle) – ↷ Page déplacée de php:chap1 à web:php:chap1 jcheron
Ligne 1: Ligne 1:
 +====== Chapitre 1 : Les bases du langage ======
  
 +===== - Types de données =====
 +
 +Pas de déclaration, typage à l'affectation
 +
 +^Données ^Type ^
 +|$i=5 |  int  |
 +|$d=1.234 |  float  |
 +|$s="chaîne" |  string  |
 +|$b=TRUE |  boolean  |
 +
 +
 +===== - Commentaires, Instructions, séparations =====
 +<sxh php>
 +//Affectation
 +$s="une chaîne";
 +/*Commentaire
 +sur plusieurs lignes*/
 +echo($s);
 +</sxh>
 +
 +
 +===== - Opérateurs =====
 +^Type ^Opérateurs ^
 +|arithmétiques | +  -  *  /  % |
 +|chaînes | .  .= |
 +|affectation | =  .=  +=  *=  -=  /= |
 +|comparaison | ==  !=  <nowiki><=</nowiki>  >=   <   >   === |
 +|incrémentation et décrémentation | $i++  $i--  --$i  ++$i |
 +|logiques | && <nowiki>||</nowiki> ! |
 +
 +===== - Structures de contrôle =====
 +
 +==== - Conditions ====
 +=== If ===
 +
 +Si... alors ...
 +<sxh php>
 +<?php
 +if ($a > $b)
 +  echo "a est plus grand que b";
 +?>
 +</sxh>
 +
 +Si... alors ... sinon ...
 +<sxh php>
 +<?php
 +if ($a > $b) {
 +  echo "a est plus grand que b";
 +} else {
 +  echo "a est plus petit ou égal à b";
 +}
 +?>
 +</sxh>
 +
 +Si ... alors ... sinon si ... sinon ...
 +
 +<sxh php>
 +<?php
 +if ($a > $b) {
 +    echo "a est plus grand que b";
 +} elseif ($a == $b) {
 +    echo "a est égal à b";
 +} else {
 +    echo "a est plus petit que b";
 +}
 +?>
 +</sxh>
 +
 +=== switch ===
 +Selon...
 +
 +Équivaut à plusieurs if sur une même variable :
 +
 +<sxh php>
 +<?php
 +if ($i == 0) {
 +    echo "i égal 0";
 +} elseif ($i == 1) {
 +    echo "i égal 1";
 +} elseif ($i == 2) {
 +    echo "i égal 2";
 +}
 +//Plus concis et lisible avec switch
 +switch ($i) {
 +    case 0:
 +        echo "i égal 0";
 +        break;
 +    case 1:
 +        echo "i égal 1";
 +        break;
 +    case 2:
 +        echo "i égal 2";
 +        break;
 +}
 +?>
 +</sxh>
 +
 +==== - Itérations ====
 +
 +=== while ===
 +
 +Tant que ...
 +
 +<sxh php>
 +<?php
 +$i = 1;
 +while ($i <= 10) {
 +    echo $i++;  /* La valeur affichée est $i avant l'incrémentation
 +                   (post-incrémentation)  */
 +}
 +?>
 +</sxh>
 +
 +=== for ===
 +
 +<sxh php>
 +<?php
 +/* exemple 1 */
 +
 +for ($i = 1; $i <= 10; $i++) {
 +    echo $i;
 +}
 +?>
 +</sxh>
 +
 +**break :** sortir de la boucle\\
 +**continue :** passer l'itération courante mais continuer à boucler
 +
 +===== - Fonctions =====
 +
 +==== - Déclaration ====
 +<sxh php>
 +<?php
 +function foo($arg_1, $arg_2, /* ..., */ $arg_n)
 +{
 +    echo "Exemple de fonction.\n";
 +    return $retval;
 +}
 +?>
 +</sxh>
 +
 +==== - Retour ou non , fonction ou procédure ====
 +<sxh php>
 +<?php
 +//procédure (sans return)
 +function affiche($a)
 +{
 +    echo "Affichage d'une variable : ".$a." et de son type : ".gettype($a);
 +}
 +?>
 +</sxh>
 +
 +==== - Arguments optionnels ====
 +<sxh php>
 +<?php
 +function affiche($s,$b=false)
 +{
 +    ...
 +}
 +?>
 +</sxh>
 +
 +==== - Appels ====
 +
 +<sxh php>
 +$bar=foo();
 +affiche("valeur");
 +</sxh>