Hudzilla.org - the homepage of Paul Hudson
Contents > Introducing PHP > How PHP is written Wish List | Report Bug | About Me ]

2.6.6     Opening and closing code islands

This is NOT the latest copy of this book; click here for the latest version.

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 most common (and recommended) manner is to use <?php to enter PHP mode, and ?> to leave PHP mode, however you can also use the short tags version, <? and ?>.

The short version has one big advantage and one big disadvantage. The advantage is that you can output information from your script by using a special short tags hack, <?=. Here is how that looks:

<?="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 much shorter, if a little harder to read. However, the big downside to the short version is that it clashes with XML, which also uses <? to open code blocks. This means that if you try to use XML and short-tagged PHP together, you'll almost certainly encounter problems - this is the primary reason people recommend using the normal open and close tags.

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, however 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 you want to and as often as you want to.





<< 2.6.5 Code blocks   2.6.7 Comments >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Steve [at] SHarvey . co . uk - 06 Sep 2008

@deathGod
Some hosts turn off the ability to use 'short tags' and ASP style tags, so require the full "<?php" expression to trigger php processing, hense the reason your 1st example may not be working as expected. If you are testing it on your own set up, check your php.ini file to see if they are enabled.

@Commenter 2
<?php
$today = date('l \t\h\e dS \of F, Y');
print "Today is ".$today."<br>\n";
?>

deathGod - 06 Sep 2008

For some reason the above example isn't working on my localhost. It just ignores the <?= example shown but executes the rest perfectly. Dreamweaver MX doesn't even pick it up as php syntax colour scheme though notepad2 does. Maybe it's system though i doubt it. php5.1.4, pws 4.0, cgi-module (as apposed to CLI), firefox browser. Great book by the way, I'm gonna recommend it to everyone whose learning php.
here is code I'm using:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?="Kello, Hitty!" ?>
<hr>
<?php
print "Hello, Kitty!";
?>
</body>
</html>

and it outputs in firefox:
nothing
horizontal rule
Hello, Kitty

this can't be a browser bug or such, but unless i messed up somewhere or this code islade syntax has been removed from php, then something must wrong with my installation of php
correct?

A PHP User - 06 Sep 2008

You might want to use examples of inserting PHP variable data in HTML statements, rather than examples of inserting string data. The string examples don't demonstrate any good reason for opening a code block, since the string could have been included in the HTML easier without PHP.

Great book so far by the way. I'm learning!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is eight plus nine?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow