Page 1 of 1

the short hand php opening tag '<?' !!!

Posted: Wed Apr 01, 2009 2:40 pm
by lauthiamkok
Very often, when I am passed on a php-based project from another developers, I come across <? as an alternative short version of the php opening tag and it never works on my local server.

I wonder why these programming background php developers tend to do this instead of typing a full tag. I am a designer background php developer.

Also, I come across this very often which puzzles me,
<?= bloginfo('template_directory'); ?>

Is anyone can advise me how to make this short hand tag <? and <?= to work on my local server? Or please advice what I can do about them? should I change these two type of short hand to <?php ;?> ?

Many thanks,
Lau

Re: the short hand php opening tag '<?' !!!

Posted: Wed Apr 01, 2009 2:58 pm
by greyhoundcode
You will probably need to adjust your php.ini settings, you can view the notes on this here.

Hopefully not teaching you to suck eggs (but you did say you found it confusing) - the <?= tag simply echoes the variable (or output from a function) that follows it:

Code: Select all

<?php $string = 'Hellooo!'; ?>
 
<?= $string; ?>
 
<?php 
    // Or, long-hand
    echo $string;
?>

Re: the short hand php opening tag '<?' !!!

Posted: Wed Apr 01, 2009 2:59 pm
by php_east
you should change them ALL to <?php.

Re: the short hand php opening tag '<?' !!!

Posted: Wed Apr 01, 2009 3:00 pm
by greyhoundcode
Worth pointing out that although many people say you should use the full tags <?php ?> other sources, such as the CodeIgniter docs, suggest otherwise.

Re: the short hand php opening tag '<?' !!!

Posted: Wed Apr 01, 2009 3:17 pm
by php_east
well, when you get your codes into a host with strict php settings where <? short cuts not allowed, with your boss/client anxiously waiting, you will know what i mean. code igniter or whatever else by that time is not in any way responsible.

it is rare, but it does happen, and you do not want to get caught in one of those nets.

Re: the short hand php opening tag '<?' !!!

Posted: Wed Apr 01, 2009 3:50 pm
by lauthiamkok
thanks for the tips guys. will amend the php.ini

as for the best practise for me, i think i wont use short hand for php - it looks amateur.

Re: the short hand php opening tag '<?' !!!

Posted: Wed Apr 01, 2009 6:54 pm
by Chris Corbyn
I have short_open_tag turned off in php.ini, then any sites that need it I drop in a .htaccess rule just for that site:

Code: Select all

php_flag short_open_tag on

Re: the short hand php opening tag '<?' !!!

Posted: Wed Apr 01, 2009 9:34 pm
by Luke
greyhoundcode wrote:Worth pointing out that although many people say you should use the full tags <?php ?> other sources, such as the CodeIgniter docs, suggest otherwise.
As does the Zend framework. I used to be majorly against the use of short open tags, but in templates, it's just much more convenient. I always use my own server, so I haven't had issues where I couldn't get short open tags turned on, but it is a perfectly valid argument. It would majorly suck if your host refused to let you have open tags and you had an app littered with them.

Re: the short hand php opening tag '<?' !!!

Posted: Wed Apr 01, 2009 10:48 pm
by divito
lauthiamkok wrote: i think i wont use short hand for php - it looks amateur.
I've always thought the opposite.
Luke wrote: It would majorly suck if your host refused to let you have open tags and you had an app littered with them.
Could just replace all and fix it in a second.

Re: the short hand php opening tag '<?' !!!

Posted: Wed Apr 01, 2009 10:56 pm
by php_east
divito wrote:Could just replace all and fix it in a second.
maybe you have clients most understanding and appreciate of your efforts to get things working.

Re: the short hand php opening tag '<?' !!!

Posted: Wed Apr 01, 2009 11:00 pm
by divito
Er, I meant using a "Replace All" function like the one in Dreamweaver, <? to <?php.

Re: the short hand php opening tag '<?' !!!

Posted: Wed Apr 01, 2009 11:04 pm
by php_east
ok, so you have clients like mine then. :)

Re: the short hand php opening tag '<?' !!!

Posted: Thu Apr 02, 2009 7:24 am
by Chris Corbyn
Luke wrote:I used to be majorly against the use of short open tags, but in templates, it's just much more convenient.
Until you need to use <?xml ?> in your template.

I agree that we can't completely remove them just yet since so many public domain apps are littered with them, but I don't think they're better than full tags and I don't think we should be using them for new projects.

I was quite surprised to see ZF advocating the use of them.

Just my $0.02.

Re: the short hand php opening tag '<?' !!!

Posted: Thu Apr 02, 2009 7:59 am
by greyhoundcode
Horses for courses and all that.

Like Divito and Luke (if I understood you guys correctly!) I quite like short tags in templates. Nice and clean and tidy, but yeah the problems with some hosts are apparent.

Thing is, the likes of the OP isn't uncommon, and you often see a lot of responses along the lines of, "No, never use short tags! They're evil!" But there are valid reasons for using them, whether you agree on a personal level or not, and so long as people get the full picture they can doubtless make their own minds up.

Re: the short hand php opening tag '<?' !!!

Posted: Thu Apr 02, 2009 8:05 am
by papa
divito wrote:Er, I meant using a "Replace All" function like the one in Dreamweaver, <? to <?php.
It's still a pain in the butt if you need to move several projects to a different configuration etc. I never use short tags.