How echo works in PHP

Daniel Pataki
Mar 4, 2009
Updated • May 28, 2017
Development
|
2

The most basic command, and probably the first you'll learn when taking a look at PHP is "echo". The first example in many books and online tutorials is the following. Create a file, give it an extension of ".php", upload it to your server, and edit it like so:

<?php
echo 'Hello World';
?>

I hate Hello World examples, but this shows what we are doing pretty well. Basically the echo command will write out the phrase "Hello World" (the quotes will not be shown, the single quotes above are part of the code), so if you open that file using Firefox for example you should simply see the phrase.

This seems straightforward, but to really understand what's happening, and to be able to work with PHP efficiently we need to dig a bit deeper to see what really happens when we echo something.

The most important thing you need to understand is that PHP is a server side language. This means that whatever code you write is never sent directly to the client (the viewer of your site for example). When someone opens a PHP file form the file is first processed by the server, and only the result is shown to the user. This is why you will never see PHP code if you view the source of a page.

You also need to know that once the server has processed the file it returns pure browser readable code. I would say pure HTML, but obviously your PHP file can contain inline javascript, just like your HTML files. Now echoing something tells the server that whatever is echoed should be placed as is into the HTML file. This means that whenever you want to put HTML tags when you are echoing you can do so by writing them as you would in an HTML file itself, like this:

<?php
echo '<strong>Hello World</strong>';
?>

Once the server has returned its result, this is downloaded by the browser and processed like usual, so your "strong" tags will be taken into account and will indeed bold text.

So what's the point of sending the server that bit of code if all it does is just put it in like it was HTML? Well, the answer lies further down the road, but basically this is helpful because you can prevent/enable specific sections of code reaching the client, so the viewer only downloads what he/she needs, not the whole file. A simple example:

<?php
$random = rand(0, 99);

if ($random > 50)
echo 'Number is above fifty';
else
&nbsp; &nbsp; echo 'Number is equal to or less than fifty';
?>

We create a variable, the value of it will be a randomly generated number between 0 and 99. If this number which we just generated is above 50 then we should echo that it is above fifty, in all other cases (it is below or equal to fifty), we should echo the other statement.

This is processed whenever someone loads (or reloads) a page, so the variable "$random" will always be different, generated "on the fly". The script then checks the number, and only the relevant piece of code is returned, so if the number generated is 55 the only piece of code that you will see in the source will be:

The number is above fifty

Obviously this is a bit over-simplified, but in real life this is basically what happens. You can also use this to generate different pieces of code for different days of the week, and the change will be automatic, you only need to program once. You can also use it to create one file to display all your posts (like in WordPress), so you don1t have to code a page for each post you write. PHP is awesome, start lovin' it!

Summary
Article Name
How echo works in PHP
Description
Daniel explains in basic terms what the PHP echo command does, why it is useful, and how you may utilize it on your own website.
Author
Publisher
Ghacks Technology News
Logo
Advertisement

Previous Post: «
Next Post: «

Comments

  1. Tobey said on March 19, 2009 at 12:04 pm
    Reply

    Nice article, thanks.

  2. Dotan Cohen said on March 5, 2009 at 7:51 pm
    Reply

    Daniel, you have a lot of bad practice usage in this post. For example the use of single quotes and the lack of brackets for each for block. Also, it’s “Hello, world!” not “Hello World”.

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.