Page 77 - Open Soource Technologies 304.indd
P. 77
Unit 3: Flow-Control Statements and PHP in Web Page
</body> Notes
</html>
Also notice that we switched between PHP and non-PHP, all in the space of a single line. PHP
instructions can be put anywhere in a file, even within valid HTML tags.
<input type=”text” name=”first_name” value=”<?php echo “XYZ”; ?>” />
When PHP is done with this text, it will read:
<input type=”text” name=”first_name” value=”XYZ” />
The PHP code within the opening and closing markers does not have to be on the same line. If
the closing marker of a PHP instruction is the last thing on a line, the line break following the
closing tag is removed as well. Thus, we can replace the PHP instructions in the “Hello, world”
example with:
<? php echo “Hello, world”; ?> <br />
with no change in the resulting HTML.
Notice that there is no trace of the PHP source code from the original file. The
user sees only its output.
3.6.2 SGML Style
The “classic” style of embedding PHP comes from SGML instruction processing tags. To use this
method, simply enclose the PHP in <? and ?>. Here’s the “Hello world” example again:
<? echo “Hello, world”; ?>
This style, known as short tags, is the shortest and least intrusive, and it can be turned off so as to
not clash with the XML PI (Process Instruction) tag in the php.ini initialization file. Consequently,
if you want to write fully portable PHP code that you are going to distribute to other people (who
might have short tags turned off ), you should use the longer <?php ... ?> style, which cannot
be turned off. If you have no intention of distributing your code, you do not have an issue with
telling people who want to use your code to turn on short tags, and you are not planning on
mixing XML in with your PHP code, then using this tag style is okay.
3.6.3 ASP Style
Because neither the SGML nor XML tag style is strictly legal HTML, some HTML editors do not
parse it correctly for colour syntax highlighting, context-sensitive help, and other such niceties.
Some will even go so far as to helpfully remove the “offending” code for you.
Mostly because you are not allowed to use a > inside your tags if you wish to be compliant, but
who wants to write code like if ($a & gt; 5)...?
However, many of these same HTML editors recognize another mechanism (no more legal than
PHP’s) for embedding code that of Microsoft’s Active Server Pages (ASP). Like PHP, ASP is a
method for embedding server-side scripts within documents.
If you want to use ASP-aware tools to edit files that contain embedded PHP, you can use ASP-
style tags to identify PHP regions. The ASP-style tag is the same as the SGML-style tag, but with
% instead of ? :
<% echo “Hello, world”; %>
In all other ways, the ASP-style tag works the same as the SGML-style tag.
LOVELY PROFESSIONAL UNIVERSITY 71