-2

Why people can use both <? and <?php? Why are they different ? What are the use cases for both (if there is any) ?

Moak
  • 12,596
  • 27
  • 111
  • 166
almaruf
  • 750
  • 7
  • 21

2 Answers2

5

<? is a short tag. They are designed to save a few keystrokes, but are more trouble than they are worth (e.g. interfering with the XML declaration) and are turned off by default in recent versions of PHP. Don't use them.

<?php is the normal way to switch from "dump code to STDOUT" to "execute code" mode.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

<? is short tag for <?php. You can use both of them as per you feel comfortable to use one of them. However I will recommend you to stick to <?php as it is the industry standard.

Now comes the question of how to enable them:

You first need to specify it in your PHP.ini (settings) that whether you want to use it or not.
Modify short_open_tag=On in your php.ini file and then restart your apache server.

If you are using a shared hosting over the internet, you cannnot edit php.ini file as they do not give access to it. But you can still solve the problem using .htaccess. Just add this code to your .htaccess file:
php_value short_open_tag 1.


Brad Larson
  • 170,088
  • 45
  • 397
  • 571