Bhupal Sapkota Computer Programmer Unraveling art, science, and commerce behind technology. Passionate about teaching web/mobile programming, writing, and growing an online business.

Bhupal's Newsletter

If you want early access and opportunities that I don’t post publicly, get on my email list.

Home

PHP Short Tag : Avoid Them

PHP Short Tag: 

it  look like this:

<?   ?>

<?= $var ?>

obvious ones:

<?php  ?>

<?php echo $var ?>.

I downloaded an open source php script, thinking to work it out for my own purpose – but what sucks is – the project uses short tag that my host doesn’t *seems* to provide support for. For the testing purpose i needed to TOGGLE the short tag on/off switch in php.ini

How to enable PHP Short Tag ?

Open your php.ini file in your favorite editor.

Here’s what it says in line number 214 through 226

; This directive determines whether or not PHP will recognize code between
; <? and ?> tags as PHP source which should be processed as such. It’s been
; recommended for several years that you not use the short tag "short cut" and
; instead to use the full <?php and ?> tag combination. With the wide spread use
; of XML and use of these tags by other languages, the server can become easily
; confused and end up parsing the wrong code in the wrong context. But because
; this short cut has been a feature for such a long time, it’s currently still
; supported for backwards compatibility, but we recommend you don’t use them.
; Default Value: On
; Development Value: Off
; Production Value: Off
; http://php.net/short-open-tag
; short_open_tag = Off

Now change the short_open_tag = Off  to (remove semicolon)

short_open_tag = On

Restart Apache or XAMPP or your WAMPP installation

but here’s a big BUT below

 

Why you should avoid using PHP Short Tag ?

Stackoverflow : Answer

Normally you write PHP like so: <?php PHP CODE HERE ?>. However if *allow_short_tags* directive is enabled you’re able to use: <? PHP CODE HERE ?>. Also sort tags provides extra syntax: <?= $var ?> which is equal to <?php echo $var ?>.

Short tags might seem cool but they’re not. They causes only more problems. Oh… and IIRC they’ll be removed from PHP6.

Stackoverflow: Answer

They’re not recommended because it’s a PITA if you ever have to move your code to a server where it’s not supported (and you can’t enable it). As you say, lots of shared hosts do support shorttags but "lots" isn’t all of them. If you want to share your scripts, it’s best to use the full syntax.

I agree that <? and <?= are easier on programmers than <?php and <?php echo but it is possible to do a bulk find-and-replace as long as you use the same form each time (and don’t chuck in spaces (eg: <? php or <? =)

I don’t buy readability as a reason at all. Most serious developers have the option of syntax highlighting available to them

 

Use it if :

  • you own a server and don’t care whether shared server support short tag or not
  • you don’t care what community says
  • you don’t want to Open Source your code later

<?php

/*

Oh please, don’t ask another question on stackoverflow about how to disable PHP Short Tag.

Rather you may want to write a parser that will loop through your 500+ php source code files and replace this Pain In The Ass PHP Short Tag to Normal PHP tag and share us the link. Here’s hint.

*/

?>

Category: technology
Tagged with:

Copy & Share

PHP Short Tag : Avoid Them
https://bhupalsapkota.com/php-short-tag-avoid-them/

Your thoughts? Please leave a comment

Your email address will not be published. Required fields are marked *