I want to give a better explanation on why the short form echo syntax in PHP annoys me so much.
A lot of people argue that PHP is a template language perfectly designed for injecting variables into themes. WordPress themes definitely are the best implementation of the model-view-controller architecture in PHP. You can see that just by looking at any template you have lying around, or just the example one on the codex. Functions return strings which can be injected into other pieces of strings pretty seamlessly. Even inside “The Loop” it’s still really easy to read what’s going on.
There’s no logic in any of those pages. Variables aren’t being defined and set. There’s no if(){}else{} in there because that’s all dealt with on the controller level, the way it should be. When you mix the view and controller you end up with ugly and unmaintainable code. It’s no longer MVC.
That’s why code like that I mentioned in my ranty blog post -
<td style="padding:0 5px 5px 0;">
<input name="Gas" type="radio" id="Gas" value="0" <?=(isset($PropertyDetail->Gas) && $PropertyDetail->Gas=="0")?"checked="checked"":""?> onclick="jQuery('#DivGasContainer').css('display','none');" checked/>No
<input name="Gas" type="radio" id="Gas" value="1" <?=(isset($PropertyDetail->Gas) && $PropertyDetail->Gas=="1")?"checked="checked"":""?> onclick="jQuery('#DivGasContainer').css('display','block');"/>Yes
</td>
- makes me so angry. This is from a project BEA Solutions has inherited from a developer who doesn’t seem to have thought this through very well. In the code above you can’t set a default “what’s checked if $PropertyDetail->Gas isn’t set?” case without making the ternary operator even more ugly than it currently is.
This project has two folders. One called /front_script/, and one called /front_form/. The controller (and accessing the model) is handled in _script/ whilst the view is handled in form/. (Both are included by index.php, which mod_rewrites every URL.) That’s a pretty decent separation.
But when you’re doing logic – complex ternary logic at that – on the view then you’ve destroyed most the benefits of developing with MVC. Which radio button should be checked needs to be decided in _script/ – the controller – not on the view.