Containers

Rather than immediately echoing the results of functions, you can store them in containers for later use. (These containers are often called “variables” by programmers.) If, for example, you were going to mention the time more than once on your web page, you might want to store the time in a container and simply echo that container everywhere on your page. Change the body of your web page to:

<h1>PHP Test Page</h1>
<?
$now = date('h:i A');
?>
<p><em>Right now</em> is <?echo $now?>. <?echo $now?> is a very important time, because <?echo $now?> is the exact time you visited our wonderful web page.</p>

The two main advantages of using containers is that it reduces errors by making it so that you only have to type the date function once, and it gives you the option of changing your mind about the format later. Suppose later on you want to change the time format to 24-hour time. You can change the date format to ‘H:i’ on the one line, $now = date('l, F jS, Y h:i A');, and it will change to showing the current weekday, the date, and the time in all three places.

We’ve put the time in a container called ‘$now’. All containers in PHP begin with the dollar sign.