Basic Syntax PHP – Part 2

By | 6 July 2008

Escaping from HTML

There are four ways of escaping from HTML and entering “PHP code mode”:

1. <? echo (“this is the simplest, an SGML processing instruction\n”;); ?>

2. <?php echo(“if you want to serve XHTML or XML documents, do like this\n”;); ?>

3. <script language=”php”>

echo (“some editors (like FrontPage) don’t

like processing instructions”;);

</script>

4. <% echo (“You may optionally use ASP-style tags”;); %>

<%= $variable; # This is a shortcut for “<%echo ..” %>

The first way is only available if short tags have been enabled. This can be done by enabling the short_open_tag configuration setting in the PHP config file, or by compiling PHP with the –enable-short-tags option to configure.

The second way is the generally preferred method, as it allows for the next generation of XHTML to be easily implemented with PHP.

The fourth way is only available if ASP-style tags have been enabled using the asp_tags configuration setting.

Note: Support for ASP-style tags was added in 3.0.4.

The closing tag for the block will include the immediately trailing newline if one is present.


Instruction separation

Instructions are separated the same as in C or perl – terminate each statement with a semicolon.

The closing tag (?>;) also implies the end of the statement, so the following are equivalent:

<?php

echo “This is a test”;

?>

<?php echo “This is a test” ?>

Comments

PHP supports ‘C’, ‘C++’ and Unix shell-style comments. For example:

<?php

echo “This is a test”; // This is a one-line c++ style comment

/* This is a multi line comment

yet another line of comment */

echo “This is yet another test”;

echo “One Final Test”; # This is shell-style style comment

?>

The “one-line” comment styles actually only comment to the end of the line or the current block of PHP code, whichever comes first.

<h1>This is an <?php # echo “simple”;?> example.</h1>

<p>The header above will say ‘This is an example’.

You should be careful not to nest ‘C’ style comments, which can happen when commenting out large blocks.

<?php

/*

echo “This is a test”; /* This comment will cause a problem */

*/

?>

Share and Enjoy
Baca :   Apache, PHP, MSSQL dan FreeTDS di Ubuntu

2 thoughts on “Basic Syntax PHP – Part 2

Leave a Reply

Your email address will not be published. Required fields are marked *

Security Comments : *

[+] kaskus emoticons