More on the PHP tags
As you saw in the previous article, you can have a normal HTML document saved with a .php extension and it will be processed as a PHP script. Any code enclosed within PHP tags inside this document will be parsed and executed. Let's see some more details on the PHP tags.
In and out of PHP
You can have as many PHP tags as you want inside the same script. So the following is perfectly valid:
<p>
Today is: <?php echo date('Y-m-d'); ?>
</p>
<p>
Server time is now: <?php echo date('H:i:s'); ?>
</p>
<p>
Script location: <?php echo __FILE__; ?>
</p>
Loading this script will give you:
Today is: 2007-09-06
Server time is now: 22:32:55
Script location: C:\mywebstuff\htdocs\php-tutorial\in-n-out.php
Note on performance and maintainability
You can go in and out of PHP as many times as you want, but that doesn't mean you should. You must keep in mind that many script tags harm the performance of the page and also make the script harder to read and maintain in the long run. It leads to the so-called "spaghetti code", named after the first computer programs that were "written" by connecting wires and often looked twisted and tangled. You can find more on good PHP programming practices in further articles, but for now keep in mind that if you find yourself going in and out of PHP too often, then it's time to rethink this part of your code.
Other PHP tags
Apart from using the <?php and ?> tags, there are other alternatives to mark the beginning and the end of a PHP code section. The use of the alternatives is discouraged, but you should know they exist, it can help you read other people's code.
The following lines are equivalent to <?php echo 'Hi!'; ?>:
<? echo 'Hi!'; ?><?= 'Hi!'; ?><% echo 'Hi!'; %><%= 'Hi!'; %>
The first two are called "short tags", enabled by default in the PHP configuration, the second two are called "ASP tags" and disabled by default. It may also be a good idea to disable short tags in the PHP configuration so that you're not tempted to use them, but this is a bit of a slightly advanced topic. Foo for now just be aware that short and asp tags exist but develop the habit of not using them, but always typing <?php ?>