Print, Echo, and Deprecated PHP Tags

One of the first unavoidable first steps of writing php is outputting to the screen using echo or print. These simple functions are invaluable to every script that I’ve ever written and I’m sure they will be all over the Zend PHP certification exam.

Turns out that in most cases, using the echo function instead of the print function is a bit faster. Usually this won’t really make any difference in your day to day code but it’s interesting nonetheless. It’s faster because it doesn’t return a value.

The print function is good when you need to use it as a function, for example:

  1. while(print("this will return stuff"))
  2. {
  3.     // Important PHP code here.
  4. }

Another basic element is the PHP script tags. I’ve really only ever used the normal tags but I’ve always known about the others. Turns out that they are all slowly being phased out.
Short tags, script tags and ASP tags are all considered deprecated and their use is strongly discouraged. Here’s an example of each type of tag that you shouldn’t be using:

  1. //Short Tags:
  2. <?
  3. code
  4. ?>
  5. <?= $variable ?>
  6.  
  7. //Script Tags:
  8. <script language=“php”>
  9. code
  10. </script>
  11.  
  12. //ASP Tags:
  13. <%
  14. code
  15. %>

Lastly I came across this odd little statement:

  1. echo (int) ((0.1 + 0.7)*10);

This will echo 7. Whaaa?

This happens because the result of this simple arithmetic expression is stored internally as 7.999999 instead of 8; when the value is converted to int, PHP simply truncates away the fractional part, resulting in a rather significant error (12.5%, to be exact).

I’ve read that the Zend exam likes to use odd little questions like this to try and trip up developers. Hopefully knowing how this works will help me on my way to becoming a Zend Certified Engineer.

 

 

2 comments

  1. Senor says:

    I was a bit confused by the statement regarding short tags. A post by Rasmus Lerdorf states that short tags will not be deprecated in PHP 6. Moreover, on the php.net it states that:

    Starting with PHP 5.4, short echo tag <?= is always recognized and valid, regardless of the short_open_tag setting.

    All of this leaves me wondering is really going to happen with short tags. Is Zend teaching that short tags will indeed be deprecated?

    • admin says:

      It was in the study guide that they were definitely being deprecated. But the study guide is a bit out of date (PHP 5.0) and it isn’t produced by Zend.

Leave a Reply

Your email address will not be published. Required fields are marked *

*