by vec · 13y ago · 0 comments
I personally try to follow a couple basic rules. .php files should be a single <?php ?> block with nothing outside it, and .phtml files shouldn't use anything but if, foreach, and already defined variables. You'll end up with code that's both cleaner and better structured.
Second, if at all possible avoid Wordpress until you've already got a firm grasp of the language. Wordpress itself has a few decisions that made sense in PHP 4 but are terrible practice now (like using global functions instead of classes) and a few others that, while not technically wrong, were questionable even then (the loop). On top of that, the quality of code in third party themes and plugins varies wildly. If you copy the techniques you see there, you'll learn some awesome tricks, but you'll also learn every bad practice in the book. Until you've got enough experience to recognize the difference, you'll do yourself more harm than good.
Unless things have changed since I last looked, PHP files are all templates. Last time I used PHP, with symphony, I actually thought PHP was a pretty reasonable templating system.
A lot of code written by beginners (myself included, once) will put business logic as close to where it will eventually be rendered as possible, so you end up with code that looks like this
<ul>
<?php
// Thirty lines of database calls and string manipulation
foreach ($results as $result): ?>
<li><?= $result ?></li>
<?php endforeach ?>
</ul>
which makes intuitive sense but quickly leads to an unreadable template. It turns out that, even though you're writing these together, you'll rarely actually want to change them together. So you eventually learn to have template files and nontemplate files.The .php vs .phtml distinction has no relevance to the compiler. They're only really useful to a human in making that template/code distinction explicit.
https://github.com/symfony/Process/blob/master/PhpProcess.ph...
Or in other words, php files are only templates if you use them that way.
Having said which, I'm confused by the lack of a closing ?>. Maybe PHP's parser lets you get away with that though.
It's completely optional.