Saturday, 9 March 2013

Why you should use Echo rather than Print and how

PHP developers have had two different functions that were always in my mind the same function.  I never really thought about which one was the better to use. I just always preferred to use the print function as I had always associated echo with a command line command so preferred the print syntax to it.  I dont know if I ever used echo in my last job (6 years) but in my new job we were drawing up our programming standards documentation.  Because we have inherited a code base thats over 10 years old.  As part of that we were discussing the usage of echo for printing content.  It turns out that the echo function can take a number of parameters. So what does that really mean.

First you need to understand a few points about concatenation.  First when you concat two strings together you end up with 3 areas of memory being used to store the information with 3 strings you get 5 seperate areas of memory allocated. Lets use an example


$hello = "hello";
$world = "world";
print  $hello . " " . $world;


Ok so both strings are 5 bytes long and when we concat them together we also put them together with a space which makes our final print statement take an area of allocated memory taking up 11 bytes of memory but and heres the kicker there is also the creation of a 1 byte string and a 6 byte string which are both held in memory.  So lets break this down and explain where they come from.

If the variable of $hello put the string at memory positions 1-5 it would use the 5 bytes in a row.  Then the creation of the second variable $world would be in memory positions 6-10 and would use the next 5 bytes of available memory (malloc function) .  Now for the creation of final string that is printed well lets take the first part of the statement  5 bytes + 1byte = a new 6 byte block of data  then we merge with the 5 bytes of the variable $world so now we have the following allocated


  1. 5 bytes holding the string "hello"
  2. 5 bytes holding the string "world" (total 10 bytes allocated)
  3. 1 byte holding the space (total 11 bytes allocated)
  4. 6 bytes holding the string "hello " (total 17 bytes allocated)
  5. 11 bytes holding the string "hello world" (total 28 bytes allocated)


Its important to know that while we have only allocated 28 bytes of information which is nothing with todays memory but think about doing this with more than two variables where your variables are hundreds of bytes long if not thousands of bytes long and if you have 10,000 visitors hitting the page then just for the memory allocation of the hello world example would use 280,000 bytes of memory.   If each of the  two variable held 100 bytes then the memory usage for 10,000 visitors at the same time would be 100+100+1+101+201 = 503 bytes * 10000 = 5,030,000 bytes  as you can see you are suddenly talking about a lot of memory allocation going on.

Garbage collection only kicks in once the page finishes executing the print statement and your memory usage will drop to the 10 bytes allocated by the defined variables as nothing is now using (pointing to) the information allocated at memory position 11 - 28.
If you just change the print statement to an echo statement you would get no benefit as you would still have to allocate the same memory for the function to be able to output the final string.

But if you REMEMBER that I said that the echo can take a list of parameters.

It turns out you can just replace the period (concat) with a comma so the code


print  $hello . " " . $world;
changes to
echo  $hello , " " , $world


With this the script has to allocate a single extra byte of information into memory (the space) and the echo  just outputs each string its given this means that it only allocates 11 bytes of information with our 10,000 visitors we are allocates 110,000 bytes and with our 100 byte strings we are now only allocating 100+100+1 = 201 bytes * 10,000 = 2,010,000.  As you can see memory allocation has decreased significantly.