2

I have a HTML file where I am using PHP like this:

...
<?=$someVariable;?>
<html code>
<?php do something ?>
<?php do something ?>
<div>
<?php do something ?>
</div>
<?php do something ?>
...

Is this a good way to go, or should I put entire HTML to PHP echo? I mean, I am starting / stopping script inside web page multiple times just because of one output one variable or if some block of the HTML code.

I am doing it this way fo better readibility of HTML/PHP code during development.

5
  • Personally I'd stick with this way. Unless PHP is frequently required within a single block of output, I prefer to open/close PHP as and when required. This, as you've realised, keeps your code more readable, and also lends it a more template feel. (Templates are all about such blocks to echo out dyanmic content as and when necessary.) Commented May 7, 2016 at 16:31
  • No reason to not do it this way. Also, well known applications do it like this, wordpress for example. Commented May 7, 2016 at 16:34
  • You may also consider using a template engine in order to make the code more readable. Commented May 7, 2016 at 16:36
  • Check this out: programmers.stackexchange.com/questions/180501/… Commented May 7, 2016 at 16:36
  • An alternative would be to keep your PHP all at the top of the file and the HTML below it. Commented May 7, 2016 at 16:36

4 Answers 4

2

Your question of "which way is better" can be broken down into two questions:

  1. Which way is more efficient?

  2. Which way is more readable?

The answer to question 1 is that the difference is negligible. PHP code is made to execute very fast on the server. Usually processes that take long on PHP would be complex functions that require iterations over large amounts of data for instance, however the actual reading of a single tag takes a very small amount of time to be processed.

The answer to question 2 depends entirely on the situation. In your situation, you are constantly adding <?php and ?> tags when you could have done it all at once, so my personal opinion would be to place it all in one echo, however there are many cases where it is more readable to place separate php elements, for example in the following form:

<form action="<?php htmlspecialchars($_SERVER['PHP SELF']);?>" method="POST">
  <?php echo $dynamic_input1'?><br>
  <input type="text" name="text1">
  <?php echo $dynamic_input2'?><br>
  <input type="text" name="text2">
</form>

Let me know if that helped.

Sign up to request clarification or add additional context in comments.

1 Comment

Accepted this because of mentioned performance that is coherent with my measurements. Only slightly slower, no big deal.
1

You should start to use templates (read: viewmodels). To keep readable the HTML you can move to logic to the top of PHP file and leave only the variable printing into the HTML code.

Comments

1

I'm not really a fan of multiple html/php blocks. What I normally use is heredoc, here's an example:

<?php
$myTitle = "Heredoc is Cool";
$myArray = array(array('someIndex' => '1st Paragraph'), array('anotherIndex' => '2nd Paragraph'));

echo <<< LOL
<!DOCTYPE html>
<html>
<head>
<title>{$myTitle}</title>
</head>
<body>
<h1>{$myArray['0']['someIndex']}</h1>
<p>{$myArray['1']['anotherIndex']}</p>
</body>
</html>
LOL;
?>

Using heredoc helps me write code without the need of concatenation, quotes and several blocks of php between html, which can be confusing sometimes. Heredoc also helps my code to be more readable.

Comments

0

This reduces the performance a little bit. too little. If you are more concerned about the readability of code, you can manage you code in different files and categorize the code (This is my personal technique). For Example a code for HTML 'Like' button will be like.txt file. Whenever I want to use the Like Button i just add

file_get_contents("like.txt");

This technique will increase flexibility of the code but again will reduce the performance a little (as you are opening files). For this technique to follow you have to maintain a class Architecture Diagram, Class Diagram, Use Case Diagram and other this.

This is the basic hurdle of programming (Quality Assurance). Increasing one thing may reduce another i.e. Increasing Interoperability and Flexibility will reduce Performance (depends how much). So you have to measure what you need the most in a particular Software project.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.