DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" " http

Transcription

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" " http
C:\Donnees\Olivier\WEB\PHP\Site_formation_OB\01_Bases.php
dimanche 6 mars 2011 18:35
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
<title> Mon site à moi </title>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/>
<!-- BALISE <link/> AUTO-FERMANTE permet de faire référence à un fichier CSS de mise en
forme -->
<link rel="stylesheet" media="screen" type="text/css"
title="Ma feuille de style à moi" href = "01_feulle_style.css"
/>
</head>
<body>
<p>
<?php
// Variable dynamique
$UneVariable=10;
$NomUneVariable = "UneVariable";
echo "\$UneVariable vaut : ", $UneVariable, "<br/>";
echo '$NomUneVariable vaut : ', $NomUneVariable, "<br/>";
echo '$$NomUneVariable vaut : ', $$NomUneVariable, "<br/>";
// Types de base
// Entier relatif : int
$EntierNeg = -10;
echo "\$EntierNeg vaut : ", $EntierNeg, "<br/>";
// Nombre décimal - Virgule flottante : float
$NbreDec = 1.23E2;
echo "\$NbreDec vaut : ", $NbreDec, "<br/>";
// Chaine de caractères : string
$ChaineCar = "Salut c'est l'été";
echo "\$ChaineCar vaut : ", $ChaineCar, "<br/>";
// Chaine de caractères : ACcès au nième caractère(attention commence à 0 !!)
echo "le 1er caractère de \$ChaineCar vaut : ", $ChaineCar [0], "<br/>";
echo "le 2d caractère de \$ChaineCar vaut : ", $ChaineCar [1], "<br/>";
// Saut de ligne
echo "AVANT saut de ligne", "\n", "APRES saut de ligne"; // ATTENTION provque un saut de ligne
dans le HTML généré
// ne génère pas de saut de ligne dans l'affichage. Il faut donc utiliser la balise <br/>
echo "<br/>";
// Conversion automatique chaine vers entier ou décimal
$StringPI = "3.14159fffzqfzqfzef";
$PI = 0;
$PI = $PI+$StringPI;
echo " la chaine \$PI vaut : ",$PI, "<br/>";
-1-
C:\Donnees\Olivier\WEB\PHP\Site_formation_OB\01_Bases.php
dimanche 6 mars 2011 18:35
// Conversion automatique chaine vers entier à zéro si aucun chiffre en début de chaine
$StringNbre = "fffzqfzqfzef";
$PI = 3.14;
$PI = $PI+$StringNbre;
echo " la chaine \$PI vaut : ",$PI, "<br/>";
// Bouléen : boolean
$Boul = false;
echo "\$Boul vaut : ", $Boul, "<br/>";
$Boul = true;
echo "\$Boul vaut : ", $Boul, "<br/>";
// Conversion explicite d'une variable bouléen ou autre type en chaine, le résultat indique si OK
$res = settype($Boul, "String");
echo "conversion réussie : ", $res, "<br/>";
echo "Conversion de \$Boul en chaine : ", $Boul, "<br/>";
// Test du type d'une variable
echo "La variable \$Boul est-elle une chaine ? ", is_string ($Boul), "<br/>";
echo "La variable \$Boul est-elle un bouléen ? ", is_bool ($Boul), "<br/>";
// Conversion d'une variable en chaine, le résultat est la valeur dans le nouveau type.
// strval retourne la valeur d'une variable après conversion en chaine
// très pratique pour les affichages sur les pages !!
$Val=2;
echo "La variable \$Val est-elle une chaine ? ", is_string ($Val), "<br/>";
$Val2=strval($Val);
echo "La variable \$Val2 est-elle une chaine ? ", is_string ($Val2), "<br/>";
echo "La variable \$Val2 vaut : ", $Val2, "<br/>";
// Idem la fonction intval renvoie la valeur d'une variable après conversion en entier
$Val3 = "1234fqffqer";
echo "La variable \$Val3 vaut : ", $Val3, "<br/>";
$Val4 = intval ($Val3);
echo "La variable \$Val4 vaut : ", $Val4, "<br/>";
$Val5 = 25.96;
echo "La variable \$Val5 vaut : ", $Val5, "<br/>";
$Val6 = intval ($Val5);
echo "La variable \$Val6 vaut : ", $Val6, "<br/>"; // prend la partie entière sans arrondir
?>
</p>
</body>
</html>
-2-