Some PHP code I look at is littered with "<?php" and "?>" tags depending on whether it's outputting HTML or not. Is there any performance benefit to this rather than using echo to write the HTML? It makes the code extremely hard to read when the code's constantly switching between code and HTML via the "<?php" tag.
Note that I'm not just talking about the occasional switchover. The code I'm currently looking at (the mantis-bt source code) is giving me a headache with the number of times it's switching. Very very hard to read.
I wonder if there's a reason for them doing it like this?
PHP is an acronym for "PHP: Hypertext Preprocessor" PHP is a widely-used, open source scripting language. PHP scripts are executed on the server.
What Does PHP Mean? The abbreviation PHP initially stood for Personal Homepage. But now it is a recursive acronym for Hypertext Preprocessor.
php and ?>. These are called PHP's opening and closing tags. Statements witihn these two are interpreted by the parser. PHP script within these tags can be embedded in HTML document, so that embedded code is executed on server, leaving rest of the document to be processed by client browser's HTML parser.
As far as readability goes, I would rather see:
<ul>
<?php foreach ($items as $item): ?>
<li>
<a href="<?php esc($item->url)?>">
<img src="<?php esc($item->icon)?>"/>
<?php esc($item->text)?>
</li>
<?php endforeach; ?>
</ul>
Than:
echo "<ul>";
foreach ($items as $item)
{
echo "<li>";
echo '<a href="'.esc($item->url).'">';
echo '<img src="'.esc($item->icon).'"/>';
echo esc($item->text);
echo '</li>';
}
echo "</ul>";
Not only that, but the latter lets your IDE of choice handle the HTML syntax and formatting (telling you, for instance, that the </a>
is missing). So, unless there's a lot more going on in-between the short bits of HTML, <?php
might be preferable.
EDIT: as for performance, anyone serious about code speed will activate a caching pre-compiler which will boil down both versions to the exact same thing.
No reason whatsoever apart from its beginners scripting, there just trying to get the results to the page without any architectural thinking or planning into the system into the long run.
What you should be doing is splitting your design up away from your logical php code, and the design compilation should be done at the end of the scripts runtime.
if you redesign the application I would certainly advise you to start with a framework because the framework will force bad habits away by its design.
Start with codeigniter and create a simple blog, understand how to connect/insert/select/update with the database, learn how to handle sessions, learn the Controllers and the principles of creating one.
After you have had a decent play about with it start looking at the poorly coded applicatioon from a distance not looking at the code or the design but yet what exactly is it doing, is it fetching results from the database, does it have a user system etc etc.
then start implementing the base layer of the application such as the above, once you have the database designed you can then start building the models to fetch from the database at the point within your application, start creating the basic view files taking samples from the pooorly coded application and recoding them within the new application, keeping in mind the structure and cleanliness of the coding.
Hope this helps you start migrating because I certainly do not advise you continue to work with an application such as that.
@mitch
Event thought you second piece of code is cleaner its still combining your view with the rest of your application where it should be like so:
<html>
<?php $this->load("segments/head"); ?>
<body>
<?php echo $this->wrap("span",$this->link("Some Linke",$this->var("homepage"))) ?>
</body>
</html>
a dedicated set of methods for the view to prevent it interacting with the main logic, this would be wrapped within an object to prevent the scope and only the object should be able to access the main logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With