The evil short open tag in php

Our client asked for a short portion of their php code to be modified. Since I'm a Java guy and so is my team lead, we asked our new developer (web developer) to do the dirty stuff for us. Good, he was able to finish quick. But then, when I tested the code he wrote, the php script would just show as plain text. It's been years since I programmed in PHP. But it's a good thing I still remember some stuff about it. From the php.ini file it said:

; Allow the <? tag.  Otherwise, only <?php and <script> tags are recognized.
; NOTE: Using short tags should be avoided when developing applications or
; libraries that are meant for redistribution, or deployment on PHP
; servers which are not under your control, because short tags may not
; be supported on the target server. For portable, redistributable code,
; be sure not to use short tags.
short_open_tag = Off


I was used to doing <?php ?> instead of  <? ?>. The short_open_tag or <? ?> makes your code less readable IMHO. It's also unsafe since short_open_tag is not enabled by default on other servers. Thanks to garri of devpinoy for pointing out that I can configure this on the server. Well, not actually the server but php.ini.
Published 02-05-2007 1:03 AM by lamia
Filed under:

Comments

Monday, February 05, 2007 1:35 PM by cruizer

# re: The evil short open tag in php

i agree. the short <? open tag in PHP is E-V-I-L! if you try to make your file go through an XML parser or something it's gonna fail.

Monday, February 05, 2007 6:19 PM by adonhis

# re: The evil short open tag in php

But sometimes php short tag could be an Angel making your coding somehow clean especially when inserting between html tags. For example:

<?php echo $var ?> will then be <?= $var ?>

Monday, February 05, 2007 7:18 PM by Garri

# re: The evil short open tag in php

just like sir cruizer said, because if you'll notice how XML files are declared <?xml version="1.0" encoding="ISO-8859-1" ?>. This can cause you problems when you work in a combination of PHP and XML.

Monday, February 05, 2007 7:37 PM by keithrull

# re: The evil short open tag in php

<?= $var ?> << yeah but this is hella confusing if you ask me..

Monday, February 05, 2007 8:37 PM by adonhis

# re: The evil short open tag in php

ahehehe! to evil or angel, it's always in user's preferences. :D

Monday, February 05, 2007 10:09 PM by lamia

# re: The evil short open tag in php

Short tags is also dangerous when you put your code in a different machine. If you don't have control over a certain machine and the people who's gonna use your code aren't sysadmins by nature then you'll receive a lot of complaints about how your code isn't doing the right thing.

You should always put in mind that the code you ship should be ready to use and would not add additional effort or hassle to the user. If it's inevitable, minimize at least.

Monday, February 05, 2007 10:46 PM by keithrull

# re: The evil short open tag in php

@adonhis: i think what you are reffering to is "coders reference". users do not technically care about how pretty your code is.. they usually look and love a program if its usable. on the other hand its the developer who created the code who cares about readabily and maintainability of the code. don't get me wrong.. i think short hand coding is good but must be used with extra care because not everybody understands it.. and! additional configuration is needed to make code coded in short hand style work on a remote server which could be an additional hurdle ans step for somebody who would use your application(just imagine if wordpress is coded using shorthand... probably 80% of web servers doesnt support it and most people wont use wordpress since it wont work with most hosting companies)

i just remembered a funny quote my officemate said last night: "you don't use bleach on every type of cloth.. it can clean some but code destroy others." :)

just my two cents.

Tuesday, February 06, 2007 10:55 AM by keithrull

# re: The evil short open tag in php

*code = could

Tuesday, February 06, 2007 3:00 PM by adonhis

# re: The evil short open tag in php

yeah, that's what i meant to it. And as a developer you should also know how will your apps run depending or considering on whatever configuration files configured to it.

about wordpress, i wonder why it wasn't working on most servers as what you've said. i've seen its source and doesn't used any php short tag since wordpress people were distributing it to the public. hmmmmm! maybe there should be something need to reconfigure first on both php and apache regardless on the default configuration of it.

thanks keith for the correction! :)

Tuesday, February 06, 2007 5:50 PM by keithrull

# re: The evil short open tag in php

hey.. i said "just imagine if wordpress is coded using shorthand..."

all software needs special configuration(e.g folder permission, user access, db access, db permission).. some are easy to configure.. some are harder.. and its up to the developer to choose the "lesser evil" since too much configuration can make users shy away from a software..

Tuesday, February 06, 2007 6:05 PM by adonhis

# re: The evil short open tag in php

oppps sorry! my bad. ahehehe