PHP Velho Oeste 2024

Evadarea din HTML

Totul ce se află în afara perechilor de tag-uri de deschidere și închidere este ignorat de către parsatorul PHP, ceea ce permite fișierelor PHP să aibă conținut mixt. Aceasta permite PHP să fie încorporat în documente HTML, de exemplu pentru a crea șabloane.

<p>Acesta va fi ignorat de PHP și afișat în browser.</p>
<?php echo 'În timp ce acesta va fi parsat.'?>
<p>Acesta va fi de asemenea ignorat de PHP și afișat în browser.</p>
Aceasta funcționează conform așteptărilor, deoarece când PHP întâlnește tag-ul de închidere, pur și simplu începe să afișeze tot ce găsește (cu excepția trecerii la linie nouă ce urmează imediat după acesta - vezi separarea instrucțiunilor) până când dă de un alt tag de deschidere, doar dacă nu este în mijlocul unei declarații condiționale, caz în care interpretatorul va determina rezultatul condiționalului înainte de a lua o decizie peste ce să sară. Vezi următorul exemplu.

Utilizarea structurilor cu condiții

Example #1 Evadare avansată folosind condiții

<?php if ($expression == true): ?>
  Aceasta se va afișa dacă expresia este adevărată.
<?php else: ?>
  Altfel aceasta va fi afișat.
<?php endif; ?>
În acest exemplu PHP va sări peste blocurile unde conționalul nu este îndeplinit, chiar dacă sunt în afara tag-urilor PHP de deschidere/închidere; PHP sare peste ele conform condițiilor, deoarece interpretatorul PHP va sări peste blocurile condiției neîndeplinite.

Pentru afișarea blocurilor mari de text, în general ieșirea din parsatorul PHP este mai eficientă decât trimiterea întregului text prin echo sau print.

De asemenea există tag-ul scurt de afișare <?= ?>.

Notă:

De asemenea rețineți că dacă încorporați PHP în XML sau XHTML va trebui să folosiți tag-urile <?php ?> pentru a vă conforma standardelor.

Example #2 Tag-uri PHP de deschidere și închidere

1.  <?php echo 'dacă vrei să folosești cod PHP în documente XHTML sau XML
                folosește aceste tag-uri'
?>

2.  Puteți utiliza tag-ul scurt de afișare pentru a <?= 'afișa aceasta' ?>.
    Aceasta este echivalent cu <?php echo 'afișa aceasta' ?>.

3.  <? echo 'acest cod este între tag-uri scurte și va funcționa doar dacă '.
            'opțiunea short_open_tag este activată'; ?>

Tag-uri scurte (exemplul trei) sunt disponibile implicit, dar pot fi dezactivate cu ajutorul directivei short_open_tag din fișierul de configurare php.ini, sau dacă PHP a fost configurat cu opțiunea --disable-short-tags.

Notă:

Deoarece tag-urile scurte pot fi dezactivate, se recomandă utilizarea doar a tag-urilor normale (<?php ?> și <?= ?>) pentru a maximiza compatibilitatea.

add a note add a note

User Contributed Notes 4 notes

up
385
quickfur at quickfur dot ath dot cx
13 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
77
ravenswd at gmail dot com
14 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";
?>
up
13
sgurukrupa at gmail dot com
10 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!
up
0
gazianis005 at gmail dot com
1 year ago
There are two types of escaping from HTML.
1.Normal escaping of using  outside of a pair of opening and closing tag
2.Advance escaping of  using conditions.

Example of normal escaping

<p>This is  going to be ignored by the php parser and displayed by the browser</p>
<?php echo 'This is going to be parsed';?>
<>This will also ignored by the php parser and displayed by the browser</>

Example of  advanced escaping

<?php if($expression == true): ?>
This will show if expression is true
<?php else : ?>
Otherwise  this will show
<?php endif ;?>
To Top