2

I have got 2 laptops with the same win7 configs. The Apache/PHP/MySQL are the copy/paste from one to the other. But the PHP works only if <?php ?> is added on one, and works fine with <? ?> on the other laptop.

I do not want to change the all PHP tags from <? ?> to <?php ?>. How do I make the PHP work with <? ?>.

hakre
  • 193,403
  • 52
  • 435
  • 836
X10nD
  • 21,638
  • 45
  • 111
  • 152
  • 3
    You point out an excellent reason to always use - you never know from one server to the next if you can get away with the ?> shortcut, so save yourself the trouble/heartache and don't use it. Imagine if you didn't have control over php.ini. – Surreal Dreams Nov 06 '10 at 05:17
  • 1
    @Surreal that's just a configuration setting, one of many. Don't make a big deal out of it. Why on the earth noone jump to yell the same warning about mod_rewrite, PDO, and every other useful technology which could be turned off, but poor short tags are always blamed? – Your Common Sense Nov 06 '10 at 05:22
  • There's more information about why – GWW Nov 06 '10 at 05:26
  • I dunno, I think I read it somewhere reputable once. You're right though, it's not a big deal - even on a hefty project, a good text editor could happily search/replace for – Surreal Dreams Nov 06 '10 at 05:26

2 Answers2

8

You need to enable short tags. Set short_open_tag to 1 in php.ini.

Prior to PHP 5.4, this enables both <? and <?= as alternatives to <?php and <?php echo respectively. In PHP 5.4, <?= is always on, so short_open_tag only controls the availability of <?.

user229044
  • 232,980
  • 40
  • 330
  • 338
6

Better idea: Change your code to use <?php. That way, it's more portable and you won't have to do the rewrite when you change servers.

If you really really want the shorthand and don't care about portability, change short_open_tag in your php.ini to 1.

EboMike
  • 76,846
  • 14
  • 164
  • 167
  • I would argue that it's *slightly* more portable and that in the unlikely event you encounter a server which doesn't have short tags enabled, you'll have to tweak an ini setting or spend 30 seconds [running your code through sed](http://www.sjhannah.com/blog/?p=118). – user229044 Nov 06 '10 at 05:20
  • You can also XML headers correctly: `` has to be `'; ?>` with short tags on. – Brendan Long Nov 06 '10 at 05:21
  • 2
    @Brendan People always mention that you can save 7 characters when opening an XML document, but never seem to care that you're typing 7 more characters *every single time* you output the result of a PHP expression. Which of these do you do more frequently? – user229044 Nov 06 '10 at 05:23
  • 1
    should I refuse PDO prepared statements usage for the same portability reason? – Your Common Sense Nov 06 '10 at 05:25
  • short_open_tag=on, changed short_open_tag=1 does not work and I have got over 200 websites, where I need to change the code, do not want to do that, works online with ?>. – X10nD Nov 06 '10 at 05:34
  • @Jean "1" and "on" are synonymous in php.ini, they will both work. – user229044 Nov 06 '10 at 05:35
  • @meagar the funny part is that I copy/paste the same config from one to the other laptop, it should work fine, but this is the only issue, I am having, no idea why. It works on the older laptop fine, but not the newer one – X10nD Nov 06 '10 at 05:38
  • @Col I did, and infact does not the "1" and "ON" work, I restarted the system itself – X10nD Nov 06 '10 at 05:57
  • @Jean well run phpinfo(), see what would it say about php.ini location and put this file there. Also make sure you didn't make other silly mistakes. Just doublecheck yourself. – Your Common Sense Nov 06 '10 at 06:01
  • Its just a copy/paste of all files including config files from one to the other laptop. Should work just fine. Actually have done it before, not sure why its not working now. Anyway let me run the phpinfo – X10nD Nov 06 '10 at 06:04
  • @col can you add the "run phpinfo(), see what would it say about php.ini location and put this file there. Also make sure you didn't make other silly mistakes. Just doublecheck yourself" as an answer so I can accept :) – X10nD Nov 06 '10 at 06:07
  • @Jean nope, I don't write answers anymore. Better accept meagar's answer. – Your Common Sense Nov 06 '10 at 06:14
  • @Jean I don't fit into this site "nicety at any cost" attitude. I do value knowledge and sense even if it makes someone unhappy. It is strictly prohibited here to make someone feel unhappy/not great. (it is not about your question of course. just in general) – Your Common Sense Nov 06 '10 at 06:55
  • Guess why I am being a dick? It's despair. When i see all these mindless answers, numerous repetitions of nonsense - it makes me sick. Yes, there are people with VERY different skill levels writing answers here. But there is not a bit of quality control also. PHP has bloated community. And this bloat reproduce itself, again and again. And SO take part in it. That's all. – Your Common Sense Nov 06 '10 at 07:26
  • That's part of the problem when you deal with a "open to everyone" community. In a workplace environment, people are expected to be above a certain skill level, so I feel more justified to make snotty comments. On SO, there are tons of questions where people can't even figure out how to use pointers. Well, if the question can be answered easily, I just do that. If they post 500 lines of mind-boggling idiot code, I just ignore the question and let someone else with more patience answer it. I'm not trying to answer every question. Keeps my blood pressure and sanity at a manageable level. – EboMike Nov 06 '10 at 07:31
  • @Col: Btw, personal question out of interest, if you don't mind - given your spelling, the hours at which you're active, and your attitude - are you German? (I'm not trying to stereotype here, I was German myself, so I'm trying to see if my spider sense is still working.) – EboMike Nov 06 '10 at 07:32
  • See, I am not on questions, but answers. Poor questions are okay, I can stand it. But these poor questions gets poor answers. A whole lot of wrong answers and not a single way to refine it. Voting system is just misused - it reflects overall "be nice" paradigm, not the real value. People just misunderstand nicety. No, I am not German :) – Your Common Sense Nov 06 '10 at 07:53
  • Dammit! And I was so sure!! My spider sense is FAILING! Anyway, I don't think people vote answers up because they're nice and friendly, but because they "look right". Hell, I sometimes vote answers up that look like they properly address the issue even though I don't know for sure myself - it looks like it works, and I think I learned something. Of course, it would be better if answers were judged by a panel of experts... but again, everything here is "open to everyone", and (almost) anyone can vote, so you have lots of bad answers to bad questions getting misguided upvotes. But who cares? – EboMike Nov 06 '10 at 08:04
  • I even have the "Unsung Hero" badge because lots of my answers didn't get a single upvote but were still accepted - lots of other, inferior (IMHO) answers got votes instead. But that's just how it is. I don't have time to be upset about it. You dinged me once for giving an answer to an idiotic question instead of pointing out how stupid the question is... and you were right (although I gladly hand a pistol to somebody who would like to shoot himself in the foot in real life). In any case -- SO isn't perfect, but I'm just taking part, trying to help, and don't think too much about it. – EboMike Nov 06 '10 at 08:07
  • @Col Stack Overflow is not a forum. **Behave professionally** is pretty much the only rule you need to follow, provided you actually know how to behave professionally. – user229044 Nov 06 '10 at 13:24