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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

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

Post 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
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

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

Post 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;
?>
Last edited by greyhoundcode on Wed Apr 01, 2009 2:59 pm, edited 1 time in total.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

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

Post by php_east »

you should change them ALL to <?php.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

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

Post 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.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

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

Post 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.
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

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

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post 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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

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

Post 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.
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

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

Post 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.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

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

Post 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.
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

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

Post by divito »

Er, I meant using a "Replace All" function like the one in Dreamweaver, <? to <?php.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

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

Post by php_east »

ok, so you have clients like mine then. :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post 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.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

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

Post 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.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

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

Post 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.
Post Reply