short tags and other php coding things

i like php’s short tags. i feel sad for people who feel they need to use the ‘<?php’ construct all the time. or worse, ‘<?php echo’ where a ‘<?=’ will do.

one part of my always-evolving personal php coding style is how i embed sql statements into my code. i used to generally do it like:

  $query = "SELECT id,name,url,rss,md5sum,method,updated AS up,"
         . "       UNIX_TIMESTAMP(lastchecked) AS lastchecked,"
         . "       UNIX_TIMESTAMP(updated) AS updated"
         . "  FROM blogs "
         . " WHERE updated > NOW() - INTERVAL 10 MINUTE AND method = 0"
         . " ORDER BY up DESC"
         . " LIMIT 10";

but lately i’ve been doing:

  $query= "SELECT id,name,url,rss,md5sum,method,updated AS up,
                  UNIX_TIMESTAMP(lastchecked) AS lastchecked,
                  UNIX_TIMESTAMP(updated) AS updated
             FROM blogs
            WHERE updated > NOW() - INTERVAL 10 MINUTE AND method = 0
            ORDER BY up DESC
            LIMIT 10
          ";

it makes it easier to cut-and-paste into the mysql client for testing.

» php, code, mysql
« april 28, 2005 6:31pmon the road again »

comments

Neil talks more about bad code readability here.

Also I'm finding it better to split long SQL queries over multiple lines like:

cursor.execute ( '''
SELECT
foo,
bar,
FROM table
WHERE
foo IS NOT NULL
''' )

» Jacques (link) » april 29, 2005 10:03am

ah, one problem with the example i picked is that it doesn’t show that i almost always use queries with placeholders.

i should dig up the most recent copy of the little mysql wrapper class i use to handle this. (which i guess is really obsolete now with mysqli, but also has some other useful convenience methods.)

and for INSERT queries, i generally prefer the INSERT INTO ... SET ... syntax over INSERT INTO ... (...) VALUES (...).

» jim (link) » april 29, 2005 10:14am

I also use your "lately" style. But pasting it to the MySQL client isn't that smooth, since I indent my code with tabs and when I paste the snippet containing tabs into the client, it tries to autocomplete (ofcourse, failing miserably).

» Indrek Siitan (link) » april 29, 2005 10:27am

So true. It's the same in Ruby; when I see something like:

def bar
return x > 0 ? true : false
end

I always want to boil it down to

def bar
x > 0
end

since Ruby returns the result of the last expression in a method.

» Tom Copeland » april 29, 2005 2:43pm

You should ALWAYS use full php tags for two reasons:

1) It's not portable. To be able to use short tags you need to turn them on in your php.ini

2) it screws up xml declarations which also start with

» GrumpySimon » september 23, 2005 10:05pm

1) anyone who turns off short tags, which are on by default, is an idiot.

2) it is stupid to disable a useful syntax just so you can avoid writing <?="<?xml ..."?> for the, at most, one time you will have an actual xml declaration in your code.

» jim (link) » september 28, 2005 11:39pm

Hello.

A bit late to the party here, but I completely agree 110% on the short tag thing. I wrote up something about this, and have been kicking myself for not promoting the original idea years ago. Essentially, the whole "short tags conflict with XML headers" is down to the parser, and I've provided a patch to remove this silly issue. When the parser's parsing, if it hits <? and the next 4 characters are "xml ", then it does not switch in to PHP mode (a T_OPEN_TAG in zend opcodes).

Feel free to read more about the patch at
http://fosterburgess.com/kimsal/?p=26

Pretty simple idea, but there's always been weird screwiness in the core PHP stuff. For example, if you look at the code for microtime(), it *explicitly* splits up a numeric value and returns the non-usable string value which people have been hacking with explode(" ") for years (only with PHP5 do we have an option of passing a parameter to get a usable numeric value!)

» michael kimsal (link) » january 15, 2006 7:33pm

hey,
if your writtin portable code, then you dont know wether or not people's hosts have php short tags enabled
so for portablity reasons its best to just to use avoid them
is that much harder htan
the only thing i can say is that the latter looks less ugly in templates
but big woop, doesnt slow u down, just makes for ugly template files

» SomeGuy » august 8, 2006 10:07am

I have a problem with short tags for the reasons stated: portability. If I am writing something that is likely to be deployed elsewhere or turned into a project, I always use long tags.

Additionally, I disagree with Tom Copeland's approach to shortening code based on language rules. I generally prefer verbose code that is more understandable than code which, completely function and short, might be harder for someone to discern the meaning.

For example, in PHP the following executes flaulessly:

$var = true ? 0 : true ? 1 : 2;

But it is difficult to figure out what the programmer is trying to figure out, whereas simply adding parenthesis can help a lot.

$var = (true ? 0 : true) ? 1 : 2;

Although not the best example, it gets the point across. On the same note, the code can be made even easier to read by writing it with if else statements.

if(true === true)
{
$var = 2;
}
elseif(true === false)
{
$var = 1;
}

Quite a dis-functional example, but again, it shows how different coding styles can make an item much easier/harder to read.

Along the same lines, recently I have been avoiding the ! operator in favor of === false.

For example, I now would use

if( is_int($int) === false ) {}

Instead of

if( ! is_int($int) ) {}

Visually, I find it easier, and when you have a large number of people working on a code base, standards like this make it much easier for everyone.

Just my 2 cents =)

» Rob Frawley (link) » march 27, 2007 3:43pm

I think it's pretty stupid to call people stupid for writing clean, portable code within the best practices put forth in the official language documentation. PHP.net specifically states that short tags are not best practice, and they will be depreciated in PHP 6 to boot.

There's stylistic differences, sure, and if you prefer short tags, go right on ahead and write your code the way you want. But to call someone "stupid" because they follow best practice, and you don't... get bent.

» Chris » april 23, 2009 5:49pm

and i think you're a moron for not being able to read the words i wrote, and see that at no point have i ever called anyone stupid for not using short tags.

and for not knowing the difference between deprecated and depreciated.

» jim winstead (link) » april 23, 2009 8:11pm

this entry is closed to new comments.