blog
HOME · CREATIVE · WEB · TECH · BLOG

Friday, September 12th, 2008

PHP 101: Echo - The command that inserts text

One of the workhorse comands in PHP is echo. A simple example looks something like this...

<?php echo rand(1,10); ?>

The output would return a number between 1 and 10 as you see on the line below (refresh the page and 90% of the time the number will change)...

3

What you see there is that echo puts into the document whatever you tell it to put into the document. If you look at the home page of NetterImages.com you can see this in action... Refresh the page and the background image will change. This was accomplished by having a line that looks like this in the stylesheet...

background-image: url(resources/collections/<?php echo $colRandom; ?>-hp-background.jpg);

Where $colRandom was previously declared using the rand() function.

Of course echo can be used to do more than insert random numbers into a document. A complex PHP application may know the name of the customer browsing the page and have the value in a variable named $customerName. You could then insert a message into the page with something like this:

<?php echo "Welcome back ".$customerName; ?>

Just remember you use period to concatenate different string values into a larger string value. So there's a period between "Welcome back " and $customerName.

Here's a more complicated example... Let's say you have a series of banner ads and you want to select one at random and insert it into your page. You assign a random number to $n and then use a if, elseif statement to write out the appropriate banner ad. A case statement might look something like this:

<?php
$n = rand(1, 18);
if ($n==1)
{
echo "<a href=\"http://www.super-store.com/\" target=\"_blank\"><img src=\"http://www.super-store.com/banners/banner-ad-1-600x125.jpg\" alt=\"Super Store\" width=\"600\" height=\"125\" border=\"0\"></a>";
}
?>

Of course you'd have more than one case in your if, elseif statement, but that's just an example.

Let's look at how the echo command was used. What you may notice is that almost everywhere there's a double quote it's written \" rather than just ". This is because the double quote is special - it starts and ends text strings, so if you want to specify a quote inside a text string you have to do what's called "escaping the character" and you escape the character by putting a backslash in front of it. So the double quote in \" isn't interpreted as the end of the string, it's seen as a double quote inside the string.

There are other special characters that can be escaped as well, here's a quick list of some a beginner might use:

  • \" (double quote)
  • \r (carriage return)
  • \\ (back slash)
  • \t (tab)

It should be mentioned that you don't always need to use echo to get text into a document. If you have text that's not going to change then you can turn PHP on and off. Here's how it would be done using the example above...

<?php
$n = rand(1, 18);
if ($n==1)
{ ?>
<a href="http://www.super-store.com/" target="_blank"><img src="http://www.super-store.com/banners/banner-ad-1-600x125.jpg" alt="Super Store" width="600" height="125" border="0"></a>
<?php }
?>

Notice that PHP was turned off momentarily, and then turned back on so all of the escaping wasn't necessary - it's just regular HTML code. In some ways that was the easiest way to do that particular example, but in other cases it's easier to use echo.

So there you have it. Echo is extremely versatile, but in some cases it's not the only way to get text into your document...

Tags:
Categories: PHP

Next Post: PHP 101: Includes »
Previous Post: « Getting Started with SEO

Leave a Reply

HOME · CREATIVE · WEB · TECH · BLOG