phpday 2027 - Call for Papers

Uscita dalla modalità HTML

Tutto ciò che si trova fuori da una coppia di tag di apertura e chiusura è ignorato dal parser PHP, il che permette di avere contenuto misto nei file PHP. Questo permette a PHP di essere incorporato nei documenti HTML, per esempio per creare modelli.

Example #1 Incorporare PHP in HTML

<p>Questo sarà ignorato da PHP e verrà visualizzato dal browser.</p>
<?php echo 'Questo invece sarà interpretato.'; ?>
<p>Anche questo sarà ignorato da PHP e verrà visualizzato dal browser.</p>

Questo codice si comporta come previsto, perché quando l'interprete PHP incontra il tag di chiusura ?>, comincia semplicemente a visualizzare tutto ciò che trova (tranne che per il ritorno a capo immediatamente successivo - vedere la separazione delle istruzioni) finché non incontra un altro tag di apertura, a meno che non si trovi nel mezzo di un'istruzione condizionale, nel qual caso l'interprete determinerà il risultato della condizione prima di decidere cosa ignorare. Vedere l'esempio seguente.

Utilizzo di strutture con condizioni

Example #2 Escape avanzato con le condizioni

<?php if (true): ?>
  Questo sarà visualizzato se l'espressione è vera.
<?php else: ?>
  Altrimenti verrà visualizzato questo.
<?php endif; ?>
In questo esempio PHP ignorerà i blocchi in cui la condizione non è verificata, anche se sono fuori dai tag di apertura/chiusura di PHP; PHP li ignora a seconda della condizione dal momento che l'interprete PHP salta i blocchi contenuti in una condizione non verificata.

Per visualizzare grossi blocchi di testo, uscire dalla modalità di parsing di PHP è generalmente più efficiente che inviare tutto il testo tramite echo o print.

Nota:

Se PHP è incorporato in XML o XHTML, devono essere utilizzati i normali tag PHP <?php ?> per rimanere conformi agli standard.

+add a note

User Contributed Notes 2 notes

up
356
quickfur at quickfur dot ath dot cx
16 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!
up
59
ravenswd at gmail dot com
17 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
//  $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
  $file_contents  = '<' . '?php die(); ?' . '>' . "\n";
?>
To Top