1

Possible Duplicate:
Are PHP short tags acceptable to use?

<?php
    //Some code
?>

or

<?
    //Some code
?>

I know the first way is the proper way but PHP code isn't validated. So, besides it saving extra typing & bytes, does it matter?

update Thanks for the replies...

  1. I had no idea they were called short tags (hence why I didn't find the duplicate SO question)
  2. I had no idea there was a specific server configuration option to allow/disallow the short tags.

Thanks again

Community
  • 1
  • 1
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370

6 Answers6

10

It does, if anyone ever uses your code on a server where short tags are disabled. I work with several servers where they are. Other than that though, no. Using the short version makes your script less portable though for the above mentioned reason. This may or may not be an issue for you.

This is another issue entirely, but related. If you are trying to generate certain types of files from PHP (XML is the candidate that comes up most often for me) then having short tags can be an issue. For instance, the following causes a PHP syntax error:

<?xml version="1.0" ?>

You must instead write the following on a server that has short tags enabled:

<?php echo '<?xml version "1.0" ?>'; ?>

Gah!

Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
  • It's unusual to see short tags disabled. Have they ever explained why they have it disabled (or why it can't be enabled)? I suppose it *may* be good to enforce a team to only use the ` – alex Oct 20 '09 at 07:01
  • 5
    I'll agree that the vast majority have them enabled. But really, the only reason to use them is laziness anyway. I prefer to use the 'long' version myself. Personally, I usually disable them on my dev servers to catch out any third party scripts I use that use them. In my fairly general experience, it's generally not a good first sign about the general quality of the script if they use it (lazy, not aware of things, whatever it happens to be). – Matthew Scharley Oct 20 '09 at 07:15
  • @Matthew Completely agree with you. – alex Oct 20 '09 at 07:36
  • How about the short-tag =$value?> inside an html template? Comes very handy! ;) – Frankie Oct 20 '09 at 12:16
  • I do use `=`. That said, I'd sooner give that up than try to fudge around with xml headers. It gets annoying when you write out what should be valid, but isn't because of a quirk in the PHP interpreter. ` – Matthew Scharley Oct 20 '09 at 12:39
4

If your project is likely to be deployed on different servers (open source software, for example) it is best to always use <?php

However, if you're like me, and always strive for maximum portability, use <?php even if you don't believe your software will ever leave your server. Most servers have short tags enabled.

However, if they have short tags disabled, and you use them, your PHP will be exposed to the world (if under the document root).

alex
  • 479,566
  • 201
  • 878
  • 984
0

No difference it's just a matter of preference I think, plus why should it matter? it's just another 3 bytes.

Edit:

Forgot to say that you have to enable short hand in php.ini

lemon
  • 9,155
  • 7
  • 39
  • 47
0

the closing bracket is not compulsory

Natrium
  • 30,772
  • 17
  • 59
  • 73
0

On some systems, the default option for the short_open_tags is off, so the latter doesn't work, while the former does, so it can completely break your website if you use the second. Personally, I just like to override the setting and use the second.

FryGuy
  • 8,614
  • 3
  • 33
  • 47
0
<? ?>

are short tags, if short tags are off it won't work.

alex
  • 479,566
  • 201
  • 878
  • 984
Geshan
  • 1,141
  • 2
  • 11
  • 21