2

I am trying to get my Mac setup as a php server, however, as successful as I have been so far, I seem to have run into a bit of bother.

My PHP opening statments are not working... but only the shorthand ones.

This works:

<?php 
  phpinfo();
?>

This doesn't:

<?
  phpinfo();
?>

It's Mac 10.5. Hope that someone can help.

Thanks

miken32
  • 42,008
  • 16
  • 111
  • 154
Tisch
  • 2,598
  • 4
  • 27
  • 34
  • You should better use clean code. – Gumbo May 18 '09 at 22:43
  • 1
    As others have mentioned, the key is enabling the short open tag option in your php.ini. However, it is strongly advised to use the full opening statement. The short one is old, ambiguous, and if I recall correctly, soon to be obsolete. If you see it, run to the hills! – dr Hannibal Lecter May 18 '09 at 22:51

3 Answers3

13

In your php.ini, set short_open_tag to On.

short_open_tag = On

From the docs:

short_open_tag boolean

Tells whether the short form (<? ?> ) of PHP's open tag should be allowed. If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?> inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version="1.0"'; ?> . Also if disabled, you must use the long form of the PHP open tag (<?php ?> ).

Edit:

short_open_tag is PHP_INI_ALL as of 5.3.0, which means that it can be changed anywhere (php.ini, .htaccess, in script). And it was PHP_INI_PERDIR before 5.3.0, which means that it can be set in php.ini and .htaccess. Therefore, you can change its value in most cases even if you don't control the server.

However, this setting is off by default. If you are going to distribute your script, it won't work on most installations out of the box. In this case, a search/replace to switch to <?php is a good idea.

Ayman Hourieh
  • 132,184
  • 23
  • 144
  • 116
  • 1
    This is the correct solution. To the question author: short_open_tag is a bad idea because you may not always have control of the server your code is run on and I would strongly recommend a quick replace on your project to use the full opening tags. – David Snabel-Caunt May 18 '09 at 22:44
  • `short_open_tags` is PHP_INI_ALL changable. – Gumbo May 18 '09 at 22:53
  • thanks. I checked that the live server has this enabled. I agree it's not good practice to do it in general, but it's ok on this project. I just thought it would be nice to get the laptop sorted so I can take my work home :) Thanks for the good advice and points though. – Tisch May 18 '09 at 23:06
0

Check if your php.ini file contains the short_open_tag=1 line.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
0

Did you verify that short_open_tag (see here) is enabled in your php.ini?

n3rd
  • 5,989
  • 4
  • 39
  • 56