Échappement depuis du HTML
Tout ce qui se trouve en dehors d'une paire de balises ouvrantes/fermantes
est ignoré par l'analyseur PHP, ce qui permet d'avoir des fichiers PHP
mixant les contenus. Ceci permet à PHP d'être contenu dans des documents
HTML, pour créer par exemple des templates.
Ceci fonctionne comme prévu parce que lorsque l'interpréteur PHP rencontre
la balise fermante ?>, il commence simplement à afficher ce qu'il
rencontre (mis à part la nouvelle ligne qui est immédiatement suivie : voir
l'
instruction
de séparation) jusqu'à ce qu'il rencontre une autre balise ouvrante
même si PHP se trouve au milieu d'une instruction conditionnelle, auquel cas,
l'interpréteur va déterminer la condition à prendre en compte avant de prendre
une décision sur ce qui doit être affiché.
Voir le prochain exemple.
Utilisation de structures avec des conditions
Exemple #1 Echappement avancé en utilisant des conditions
<?php if ($expression == true): ?>
Ceci sera affiché si l'expression est vrai.
<?php else: ?>
Sinon, ceci sera affiché.
<?php endif; ?>
Dans cet exemple, PHP va ignorer les blocs où la condition n'est pas remplie,
même si ils sont en dehors des balises ouvrantes/fermantes de PHP. PHP
va les ignorer suivant la condition vu que l'interpréteur PHP va passer
les blocs contenant ce qui n'est pas remplie par la condition.
Pour afficher de gros blocs de texte, il est plus efficace de sortir
du mode d'analyse de PHP plutôt que d'envoyer le texte via la fonction
echo ou
print.
Note:
Si PHP est intégré dans un document XML ou XHTML, la balise PHP
standard <?php ?>
doit être utilisé pour rester
conforme aux standards.
quickfur at quickfur dot ath dot cx ¶15 years ago
When the documentation says that the PHP parser ignores everything outside the <?php ... ?> tags, it means literally EVERYTHING. Including things you normally wouldn't consider "valid", such as the following:<html><body><p<?php if ($highlight): ?> class="highlight"<?php endif;?>>This is a paragraph.</p></body></html>Notice how the PHP code is embedded in the middle of an HTML opening tag. The PHP parser doesn't care that it's in the middle of an opening tag, and doesn't require that it be closed. It also doesn't care that after the closing ?> tag is the end of the HTML opening tag. So, if $highlight is true, then the output will be:<html><body><p class="highlight">This is a paragraph.</p></body></html>Otherwise, it will be:<html><body><p>This is a paragraph.</p></body></html>Using this method, you can have HTML tags with optional attributes, depending on some PHP condition. Extremely flexible and useful!
ravenswd at gmail dot com ¶16 years ago
One aspect of PHP that you need to be careful of, is that ?> will drop you out of PHP code and into HTML even if it appears inside a // comment. (This does not apply to /* */ comments.) This can lead to unexpected results. For example, take this line:<?php $file_contents = '<?php die(); ?>' . "\n";?>If you try to remove it by turning it into a comment, you get this:<?php?>' . "\n";?>Which results in ' . "\n"; (and whatever is in the lines following it) to be output to your HTML page.The cure is to either comment it out using /* */ tags, or re-write the line as:<?php $file_contents = '<' . '?php die(); ?' . '>' . "\n";?>
sgurukrupa at gmail dot com ¶11 years ago
Although not specifically pointed out in the main text, escaping from HTML also applies to other control statements:<?php for ($i = 0; $i < 5; ++$i): ?>Hello, there!<?php endfor; ?>When the above code snippet is executed we get the following output:Hello, there!Hello, there!Hello, there!Hello, there!