PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links 
search for in the  
previousR�f�renceLe s�parateur d'instructionnext
Last updated: Tue, 09 Jul 2002
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Chinese | Czech | Dutch | Finnish | German | Hungarian | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Swedish | Turkish

Chapitre 5. La syntaxe de base

Le passage du HTML au PHP

Lorsque PHP commence � traiter un fichier, il ne fait qu'afficher le texte HTML qu'il rencontre. Si vous renommez un fichier .html en .php, il s'affichera exactement comme avant.

Si vous voulez ins�rer des commandes PHP dans votre fichier, vous devez indiquer � PHP le d�but d'une telle s�quence, en passant en mode PHP. Il y a quatre moyens pour passer du mode HTML au mode PHP :

Exemple 5-1. Le passage du HTML au PHP

1.  <? echo ("Ceci est un exemple d'affichage � l'�cran en PHP, sous forme d'expression SGML.\n"); ?>
    <?= expression ?>  Raccourci de "<? echo expression ?>"
2.  <?php echo("Si vous voulez afficher du XML ou du XHTML, faites comme ceci.\n"); ?>
3.  <script language="php">
        echo ("Certains �diteurs HTML (comme FrontPage)
         n'acceptent pas les expressions telles que celle-ci.");
    </script>
4.  <% echo ("Vous pouvez aussi utiliser le style ASP comme d�limiteur."); %>
    <%= $variable; # ceci est un raccourci pour  "<%%echo .." %>

La deuxi�me m�thode est g�n�ralement utilis�e, car elle permet une impl�mentation ais�e de PHP avec la prochaine g�n�ration de XHTML.

La premi�re possibilit� n'est valable que si vous l'avez activ�e. Soit en faisant appel � la fonction short_tags() (NdT : semble avoir disparu), soit en utilisant l'option d'ex�cution short_open_tag dans le fichier de configuration, soit en utilisant l'option de compilation --enable-short-tags.

La quatri�me possibilit� est seulement disponible si vous l'avez activ�e en utilisant soit l'option d'ex�cution asp_tags, soit en utilisant l'option de compilation --enable-asp-tags.

Note�: Le support de la quatri�me possibilit�, ASP-style, a �t� ajout� dans la version 3.0.4.

La marque de fermeture d'un bloc (?>) comprend la nouvelle ligne suivante, s'il y en a une.

PHP vous permet d'utiliser des structures telles que :

Exemple 5-2. M�thode avanc�e

<?php
  if ( expression-bool�enne ) {
?>
  <strong>Ceci est vrai.</strong>
<?php
  } else {
?>
<strong>Ceci est faux.</strong>
<?php
  }
?>
Cela fonctionne comme on peut s'y attendre, car PHP traite le texte entre ?> et <?php comme une fonction echo(), sans remplacer les variables �ventuelles par leur valeur.

User Contributed Notes
La syntaxe de base
add a note about notes
[email protected]
03-Jan-2001 08:35

[Ed Note: this was fixed in 4.0.5 [email protected]]

If you're using a MacOS text editor, such as BBEdit, for composing your PHP, you'll want to save your files with Unix line breaks.
This fixes the "error on line 1" problem where PHP's error system doesn't recognize the MacOS line breaks properly. It will also allow you more flexibility in creating multi-line SQL statements.

11-Oct-2001 06:34
I use FrontPage 2000 exclusively for creating my PHP documents. I use the <% %> style syntax. I can put code anywhere, before head (as is required for cookie data) and anywhere else in between. Using advanced flow control, i can design an entire page in html, and then put PHP control statements around it. This is much easier than <% echo %> commands. You can drop in/out of PHP at ANY time, and the only reason to ever use <% echo %> is for variables. Save yourself some hassle and write your html in html and avoid the echo mess.
[email protected]
12-Dec-2001 06:36

[Ed Note:
This is because of short_tags, <?xml turns php parsing on, because of the <?.
[email protected]]

I am moving my site to XHTML and I ran into trouble with the <?xml ?> interfering with the <?php ?> method of escaping for HTML. A quick check of the mailing list confirmed that the current preferred method to cleanly output the <?xml ?> line is to echo it:

<?php echo("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); ?>

[email protected]
16-Jan-2002 07:40

Under php 3.0.14 :
if you have a line like this :
echo "?> written to file";
And you wish to comment this line, you'll do this :
//echo "?> written to file";
But this will generate a parse error at the end of script (even if this line was in an include script).

Why? because '?>' is ignored as long as it is inside "".
But once you've commented, the echo function is ignored, and '?>' takes its signification : end of script!

I guess it was corrected on next version, but if you run under php 3.0.14 be careful, it make me loose a lot of time!

Paquerette

dave@[nospam].netready.biz
18-Mar-2002 10:21

A little "feature" of PHP I've discovered is that the <?PHP token requires a space after it whereas after the <? and <% tokens a space is optional.

The error message you get if you miss the space is not too helpful so be warned!

(These examples only give a warning with error_reporting(E_ALL) )

<?PHP/*<Some HTML>*/?> fails...
<?/*<Some HTML>*/?> works...

[email protected]
17-Jun-2002 06:50

I can't find out how to break the line in the middle of a function. I have tried the standard Unix '\' continuation trick, but that doesn't work.
[email protected]
18-Jun-2002 08:51

if you're experiencing problems with php PIs when generating creating mixed php/html content with e.g. an XSLT processor in html output mode:
it's not the processors fault.
an _SGML_ processing instruction is actually written as <?php ..>, i.e. without a trailing question mark.

<xsl:processing-instruction name="php">
echo $hello;
</xsl:processing-instruction>
will therefor not not work like it should.

a sane solution to work around this is generating <script> tags instead.

[email protected]
09-Jul-2002 08:42

also, if you're using Mac OS X...i highly suggest looking into Dreamweaver MX. it's been a DREAM in learning and using PHP. no problems with line breaks. it also colors your code based on the codes type and has pop-up tips on what goes in certian brackets and functions. really sharp...
[email protected]
15-Jul-2002 10:37

Need help how I can build a mysql fot php to get a localhost ??
15-Jul-2002 03:42
You don't need the closing tag if you don't plan to add html (or something) afterwards. This works (but the ; must be there):

<? phpinfo();

[email protected]
15-Jul-2002 06:45

Ich habe hier einen Code der mir egentlich sagen m�sste: Ihr letzter versuch war am...
Er funktioniert aber nicht, deshalb gebe ich in hier rein, falls mir irgendjemand diesen Code richtigstellen kann, sollte er mir bitte ein mail schicken.
Hier der Code:

<?php
$lastvisit = $http_COOKIE_VARS
[,,lastvisit��];
if (!$lastvisit)
{
echo ,,Sie haben uns in diesen Monat noch nicht beehrt!
��;
}
else
{
echo ,,Ich letzter Besuch war am: $lastvisit��;
}
$datum = date(,,d.m.Y H:i:s��)
setcookie(lastvisit��, $datum,
time()+30*24*60*60); // 30 tage lang g�ltig
?>

add a note about notes
previousR�f�renceLe s�parateur d'instructionnext
Last updated: Tue, 09 Jul 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Thu Aug 29 20:06:18 2002 CEST