Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - To echo or not to echo?

What is more efficient and/or what is better practice, to echo the HTML or have many open and close php tags?

Obviously for big areas of HTML it is sensible to open and close the php tags. What about when dealing with something like generating XML? Should you open and close the php tags with a single echo for each piece of data or use a single echo with the XML tags included in quotations?

like image 828
Mark Avatar asked Feb 07 '10 13:02

Mark


People also ask

Which is faster echo or print in PHP?

They are both used to output data to the screen. The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print .

Does echo work in PHP?

PHP echo statement can be used to print the string, multi-line strings, escaping characters, variable, array, etc. Some important points that you must know about the echo statement are: echo is a statement, which is used to display the output. echo can be used with or without parentheses: echo(), and echo.

What can I use instead of echo in PHP?

The PHP print Statement You can also use the print statement (an alternative to echo ) to display output to the browser. Like echo the print is also a language construct not a real function. So you can also use it without parentheses like: print or print() .

What is the difference between echo and return in PHP?

Echo is for display, while return is used to store a value, which may or may not be used for display or other use.


2 Answers

PHP solves this problem by what is known as heredocs. Check it out please.

Example:

  echo <<<EOD
  <td class="itemname">{$k}s</td>
  <td class="price">{$v}/kg</td>
EOD;

Note: The heredoc identifer (EOD in this example) must not have any spaces or indentation.

like image 28
Sarfraz Avatar answered Oct 08 '22 10:10

Sarfraz


From a maintenance perspective, one should have the HTML / XML as separate from the code as possible IMO, so that minor changes can be made easily even by a non-technical person.

The more a homogeneous block the markup is, the cleaner the work.

One way to achieve this is to prepare as much as possible in variables, and using the heredoc syntax:

// Preparation

$var1 = get_value("yxyz");
$var2 = get_url ("abc");
$var3 = ($count = 0 ? "Count is zero" : "Count is not zero");
$var4 = htmlentities(get_value("def"));

// Output  

echo <<<EOT

 <fieldset title="$var4">
   <ul class="$var1">
     <li>
       $var2
     </li>
   </ul>
  </fieldset>

EOT;

You will want to use more sensible variable names, of course.

Edit: The link pointed out by @stesch in the comments provides some good arguments towards using a serializer when producing XML, and by extension, even HTML, instead of printing it out as shown above. I don't think a serializer is necessary in every situation, especially from a maintenance standpoint where templates are so much more easy to edit, but the link is well worth a read. HOWTO Avoid Being Called a Bozo When Producing XML

Another big advantage of the separation between logic and content is that if transition to a templating engine, or the introduction of caching becomes necessary one day, it's almost painless to implement because logic and code are already separated.

like image 177
Pekka Avatar answered Oct 08 '22 09:10

Pekka