Reputation: 9527
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.
Upvotes: 2
Views: 624
Reputation: 7113
Your question of "which way is better" can be broken down into two questions:
Which way is more efficient?
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.
Upvotes: 2
Reputation: 98961
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.
Upvotes: 1
Reputation: 988
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.
Upvotes: 1
Reputation: 113
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.
Upvotes: 0