PHP 8.2.0 RC7 available for testing

É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.

<p>Ceci sera ignoré par PHP et affiché au navigateur.</p>
<?php echo 'Alors que ceci sera analysé par PHP.'?>
<p>Ceci sera aussi ignoré par PHP et affiché au navigateur.</p>
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, au quel cas, l'interpréteur va déterminer la condition à prendre en compte avant de prendre une décision sur ce qui doit être afficher. 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.


Si PHP est intégré dans un document XML ou XHTML, la balise PHP standard <?php ?> doit être utilisé pour rester conforme aux standards.

add a note

User Contributed Notes 11 notes

quickfur at quickfur dot ath dot cx
12 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:

<p<?php if ($highlight): ?> class="highlight"<?php endif;?>>This is a paragraph.</p>

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:

<p class="highlight">This is a paragraph.</p>

Otherwise, it will be:

<p>This is a paragraph.</p>

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
13 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 die(); ?>' . "\n";

If you try to remove it by turning it into a comment, you get this:

//  $file_contents  = '<?php die(); ?>' . "\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 die(); ?' . '>' . "\n";
sgurukrupa at gmail dot com
8 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!
snor_007 at hotmail dot com
12 years ago
Playing around with different open and close tags I discovered you can actually mix different style open/close tags

some examples

//your php code here


<script language="php">
//php code here
anisgazig at gmail dot com
3 years ago
Version of  7.0.0,3 tags are available in php.
1.long form tag (<?php ?>)
2.short echo tag(<?= ?>)
3.short_open_tag(? ?)
You can use short_open_tag when you start xml with php.
mike at clove dot com
11 years ago
It's possible to write code to create php escapes which can be processed later by substituting \x3f for '?' - as in echo "<\x3fphp echo 'foo'; \x3f>";

This is useful for creating a template parser which later is rendered by PHP.
9 months ago
Example for a basic implementation of ternary operator to validate and print a class in the layout.

<!DOCTYPE html>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= $page_title ?></title>
<body <?= strtolower($page_slug) === 'homepage' ? 'class="page-homepage"' : ''; ?>>
        <h1><?= $page_title ?></h1>
        <p>This is a paragraph.</p>
2 years ago
When the PHP interpreter hits the ?> closing tags, it WON'T output right away if it's inside of a conditional statement:
(no matter if it's an Alternative Syntax or not)

= 1;
$b = 2;
if (
$a === 1) {
    if (
$b == 2) {
} else {

This would output `<html><head></head></html>`.
Aside from conditional statements, the PHP interpreter also skip over functions! What a surprise!

function show($a) {
    <a href="https://www.<?php echo $a ?>.com">
    <?php show("google") ?>

This gives `<html><body><a href="">Link</a></body></html>`.
These really confused me, because at first I thought it would output any HTML code right away, except for Alternative Syntaxes ( There are more strange cases than I thought.
1 year ago
Since it's not documented (AFAICT) and it might cause confusion: a single line break immediately after ?> is ignored. Since whitespace is hard to see, whitespace is replaced with _ and the following code

<?php echo '1'; ?>
echo '2'; ?>_
<?php echo '3'; ?>
_<?php echo '4'; ?>_<?php echo '5'; ?>

will produce

2 years ago
<p>This is ignore by the php parser and displayed by the browser </p>

<?php echo "While this is going to be parsed"; ?>


when php interpreter hits the closing tag it start to outputing everything whatever it finds until it hit another opening tag
.If php interpreter find a conditional statement in the middle of a block then php interpreter decided which block skip 

Advanced escaping using conditions

<?php $a = 10; if($a<100): ?>
  This conditional block is executed
  <?php else: ?>
      otherwise this will be executed
      <?php endif; ?>

In php 5 version,there are 5 opening and closing tags.
1.<?php echo "standard long form php tag and if you use xml with php this tag will be use";?>

2.<?= "short echo tag and alwayes available from 5.4.0";?>

3.<? echo "short open tag which is available if short_open_tag is enable in php ini configuration file directive or php was configured with --enable-short-tags.This tag has discoursed from php 7.If you want to use xml with php,then short_open_tag in php ini will be disabled";?>

4.<script language="php">
echo "Some editor do not like processing the code within this tag and this tag is removed from php 7.0.0 version";


5.<% echo "asp style tag and asp_tags should be enabled but now php 7.0.0 version,this tag is removed";%>
Emil Cataranciuc
4 years ago
"<script language="php"> </script>, are always available." since PHP 7.0.0 is no longer true. These are removed along the ASP "<%, %>, <%=" tags.
To Top