從HTML中分離PHP, 在HTML種寫PHP代碼
阿新 • • 發佈:2017-09-06
解析 是否 other 使用方式 while 移植 splay 配置 可移植性
既有PHP代碼又有HTML代碼的文件必須以" .php "結尾.
為了使php解釋器忽略HTML代碼, php只處理一對PHP開始和結束標記之間的內容.
如:
<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遇到"<?php"標記的時候開始解析, 遇到“ ?> ”的時候結束解析. 而其他的代碼則保持原樣.
這種情況只有一個php開始以及結束標記, 如果有多組php標記, 那麽為於兩組php標記之間的代碼是否會保持原呢?
如:
<?php if ($expression == true): ?> This will show if the expression is true. <?php else: ?> Otherwise this will show. <?php endif; ?>
上述代碼只會輸出" This will show if expression is true "或者 “ Otherwise this will show ”中的一個, 而另一個則會被忽略. 可以看輸出PHP將會自動跳過條件語句中未達成的段落.
PHP種開始和結束標誌四種不同的寫法:
1 <?php ?>
2 <script language="php"> </script>
3 <? ?>
4 <% %>
其中1、2兩種方式總是可以使用的.
方式3為段標記, 方法4為ASP風格, 使用時必須在php.ini種配置
short_oepn_tag= true 支持識別短標記 、
asp_tags=true 支持識別ASP風格的標記
一般為了保持代碼的可移植性建議使用方式1或者2.
從HTML中分離PHP, 在HTML種寫PHP代碼