Thursday 18 April 2013

PHP: jumping in and out of code is easy

G'day:
Now I will get back to discussing those PHP tutorials I did at some point soon, but I have a fairly fractured day today, so I don't have heaps of spare time for the blog. So here's a quicky.

In CFML, a lot of people have traditionally kept away from CFScript, kinda screwing up their noses at it, and preferring tags for all their code. One of the points that was made in the past that there was a lot of pretty essential CFML functionality that wasn't possible to use in CFScript because it was all wrapped up in a tag-only solution. ColdFusion 9 & 10 (and of course Railo too) have pretty much dealt with probably 95% of the stuff one would want to do in CFML in CFScript now, which makes that reasoning a bit obsolete.


What this means is a lot of people are now using CFScript more than they used to, but sometimes get their brains stuck in the tag paradigm and want to mishmash script-syntax and tag-syntax together. I think if one is writing code that way, then some rethink of the job at hand is probably a better thing to be doing, but still: people want it.

Also, for some things a tag approach is simply better: ColdFusion's solution to querying via Query.cfc is godawful, and just when I thought there couldn't be a worse approach, along comes Railo with their take on it. Another example is <cfsavecontent> is superior to the savecontent approach, and <cfxml> is visually more understandable than using XML functions I reckon. So there's a case for wanting to have the best of both worlds. or stuff like <cflogin> which doesn't have equivalent script-ready implementation.

Personally I'd probably always abstract the stuff that needs to be done via tags into an included file or a function, but the fact remains there's a case for it.

Often one can jump in and out of a <cfscript> block and implement the tags that way, but this requires the script block to be syntactically complete, so sometimes it's not possible to do. Consider this pseudocode:

<cfscript>
    if (someCondition){
        // lots of script code
        
        // now I need to do a <cfquery> / <cfsavecontent> / <cflogin>
    
        // more script code
    }
</cfscript>

Damn. On CFML, one cannot do this:

<cfscript>
    if (someCondition){
        // lots of script code
</cfscript>        
<cfquery>
    SELECT    col
    FROM    tbl
</cfquery>
<cfscript>    
        // more script code
    }
</cfscript>

It'd be bloody handy if one could. I suspect that CFScript blocks are parsed (and perhaps even compiled) separately, rather than the code for the entire file being parsed and then compiled. So the CFScript needs to be syntactically complete.

Here's the good thing about PHP. One can do that! Now I can't do a like-for-like example here, but this code demonstrates it:

<?php for ($i=1; $i <= 10; $i++){ ?>
    <p>G'day World!</p>
<?php } ?>

See how I start the for loop in one code block, and complete it in another code block. Cool!

Now I imagine this sort of thing (especially an example as egregious as that), would not be encouraged in PHP - although I don't know - but it's doable. And for every "best practice" that says to do/not do something, there'll be an exception where it makes sense.

And given CFML has a bunch of stuff that's just better in tags - along with a bunch of stuff that's better in CFScript - wouldn't it be cool if CFML could be parsed like this? That'd be the best solution compared to a bunch of poor suggestions for solving this script/tag mix-up requirement.

Nice one.

--
Adam