Section 4.7. Opening and Closing Code Islands


4.7. Opening and Closing Code Islands

There are many ways to open a PHP code island (to enter PHP parsing mode), and you are welcome to choose which you prefer. The recommended manner is to use <?php to enter PHP mode, and ?> to leave PHP mode, but you can also use the short tags version, <? and ?>.

The short version has one big advantage and two big disadvantages: you can output information from your script by using a special short tags hack, <?=, like this:

     <?="Hello, world!" ?> 

Here is the equivalent, written using the standard open and closing tags:

     <?php             print "Hello, world!";     ?>

As you can see, the short tags version is more compact, if a little harder to read. However, the first downside to the short version is that it clashes with XML (and therefore XHTML), which also uses <? to open code blocks. This means that if you try to use XML and short-tagged PHP together, you will encounter problemsthis is the primary reason people recommend using the normal open and close tags. Short tags are always dangerous because they can be disabled in the PHP configuration file, php.ini, which means your scripts may not be portable.

Two other, lesser-used variants exist: <% %>, which opens and closes code blocks in the same way as Microsoft's ASP, and also <script language="php"></script>. These two often work better with visual editor programs such as Dreamweaver and FrontPage, but they are not recommended for general use because they need to be enabled to work.

You can switch into and out of PHP mode by using <?php and ?> whenever and as often as you want to.



PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net