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!Излез од HTML
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
Излез од HTML
Референца за `language.basic-syntax.phpmode.php` со подобрена типографија и навигација.
Излез од HTML
Се што е надвор од пар отворачки и затворачки тагови се игнорира од PHP парсерот, што дозволува PHP датотеките да имаат мешана содржина. Ова му дозволува на PHP да биде вграден во HTML документи, на пример за креирање шаблони.
Пример #1 Вградување PHP во HTML
<p>This is going to be ignored by PHP and displayed by the browser.</p>
<?php echo 'While this is going to be parsed.'; ?>
<p>This will also be ignored by PHP and displayed by the browser.</p>Ова работи како што се очекува, бидејќи кога PHP интерпретерот ќе ги погоди ?> затворачките тагови, тој едноставно почнува да го прикажува се што ќе најде (освен веднаш следниот нов ред - види одвојување на инструкции) додека не погоди друг отворачки таг, освен ако не е во средина на условен исказ, во кој случај интерпретерот ќе го утврди исходот од условот пред да донесе одлука што да прескокне. Види го следниот пример.
Користење структури со услови
Пример #2 Напредно бегство со користење услови
<?php if ($expression == true): ?>
This will show if the expression is true.
<?php else: ?>
Otherwise this will show.
<?php endif; ?>За прикажување големи блокови текст, исклучувањето од PHP режим на парсирање е генерално поефикасно отколку испраќање на целиот текст преку echo or print.
Забелешка:
Ако PHP е вграден во XML или XHTML, нормалниот PHP
<?php ?>мора да се користи за да остане во согласност со стандардите.
Белешки од корисници 2 забелешки
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";
?>