TOC
Basic PHP:

PHP tags

In the last chapter, we used those magic PHP tags. You may save your files with the .php extension, but that's not enough. Sure, when you use the appropriate extension, as defined in the config file of the webserver, PHP will look at the file. But only the stuff between the PHP tags are actually interpreted - the rest is just ignored. This allows you to mix HTML and PHP in the same file, and while other technologies like ASP.NET tries to separate code and markup, PHP still encourages this mix, and allows you to do it in several different ways.

In the previous chapter, we used the most common and most correct way of starting the PHP block:

<?php [code here] ?>

Another version is this one, which is the same kind of tags used for e.g. blocks of JavaScript code:

<script language="php"> [code here] </script>

Those two options are always available. However, a lot of PHP installations are set up to allow the short version as well:

<? [code here] ?>

On some servers, ASP style tags have been enabled. They look like this:

<% [code here] %>

Since this is not necessarily supported on all servers, you may wish to use the full version instead.

The same goes for the special output version, which looks like this:

<?= [output here] ?>

The last one is the same as writing:

<?php echo "some output"; ?>

As you can see, you do save some keystrokes, and if you're writing code for your self only, you may be tempted to use the short versions instead. Just be aware that if you ever change to another server, the last two options may be disabled.

In and out of PHP

When writing webpages, you naturally use HTML all over the place. You will quickly need to combine HTML tags with output generated from PHP. Now that's easy:

<?php
$myVar = "Hello world!";
echo "<b>" . $myVar . "</b>";
?>

We simply create a string consisting of three parts: The starting bold tag, our variable, and then the ending bold tag. Using the dot operator, which concatenates strings in PHP, we output the entire thing as if it was just another piece of text. In simple cases like this one, our above example is just fine. However, if you suddenly start having way more plain HTML, with a minimum of PHP code required in it, the above approach is actually not the best way to go. You may do it like this instead:

<?php
$myVar = "Hello world!";
?>
<i>We have HTML <u>all</u> over the place here!</i><br />
<i>But some PHP as well, as you can see:</i><br />
A message from PHP: <b><?php echo $myVar; ?></b>

In this tiny example, we have way more plain HTML and text than we have PHP code. It makes sense to jump out of PHP in those cases, and according to the PHP manual, it even offers better performance than outputting huge amounts of HTML through PHP. So we can jump out of it, but can we jump in again? Of course. And PHP is actually quite flexible in this area, as the following example will show you:

<?php
$myVar = 42;
if($myVar == 42)
{
    ?>
    <b>Yes, the number is in fact 42!</b>    
    <?php
}
?>

As you can see, we jump out of PHP in the middle of a conditional block of code. PHP understands this just fine. The HTML and text part is only outputted if $myVar is actually set to 42. Try changing it and you will see that PHP respects the condition even when you mix in plain HTML and text.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!