Alex Rivera | Logout

Heavy use of PHP's "<?php" tag

Asked 2011-01-13T16:04:18.257
10

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?

Edit
Report

1 Answer

3

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 dedi

answered 2011-01-13T16:12:42.573

Your Answer