How echo works in PHP

Status
Not open for further replies.

Osiris

Golden Master
Messages
36,817
Location
Kentucky
How echo works in PHP

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 that 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 next 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 retruns 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 wil 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 of section 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
    echo ‘Number is equal to or less than fifty';
?>
Without really understanding what'd going on I can still explain. 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!
 
Status
Not open for further replies.
Back
Top Bottom