<?php ... ?> vs. <? ... ?> error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
empiresolutions
Forum Newbie
Posts: 20
Joined: Tue Apr 11, 2006 10:39 am
Location: Portland, Or

<?php ... ?> vs. <? ... ?> error

Post by empiresolutions »

I have just upgraded my LAMP server to PHP 5.1.1. Now my all my pages on all my sites are messed up. I have figured out that it is because I donot use <?php ... ?> to write php in. i just use <? ... ?>. Seems without the "php" in the tag it will not display properly.

Is there a switch in the php.ini file or in apache that needs to be tripped so that it accepts all my thousands of legacy php tags?

Thank you, Cesar
empiresolutions
Forum Newbie
Posts: 20
Joined: Tue Apr 11, 2006 10:39 am
Location: Portland, Or

Post by empiresolutions »

ok got it, update php.ini as follows.

*php.ini becomes php.ini.rpmsave in PHP 5*

; Allow the <? tag. Otherwise, only <?php and <script> tags are recognized.
; NOTE: Using short tags should be avoided when developing applications or
; libraries that are meant for redistribution, or deployment on PHP
; servers which are not under your control, because short tags may not
; be supported on the target server. For portable, redistributable code,
; be sure not to use short tags.
short_open_tag = On

Now it works!! Woohoo.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

short_tags which is a php.ini directive.

It is best to alter all of your files to add the correct tags however. This can be done via a script or command-line fairly easily, although it does rely a bit on how you vary in the usage:

Code: Select all

[feyd@home]> php -r "foreach(glob('*.php') as $file) { file_put_contents($file, preg_replace(array('#<\?\s*=#s', '#<\?(\s*)#s'), array('<?php echo ', '<?php$1'), file_get_contents($file))); }"
It may need a little tweaking depending on if I wrote it right (did it from memory) and based on how your write your code most often, but should work for most instances. Run that in each directory you have scripts in that use short tags.
empiresolutions
Forum Newbie
Posts: 20
Joined: Tue Apr 11, 2006 10:39 am
Location: Portland, Or

Post by empiresolutions »

i understand that short tags is not the best way to write and i will change from here on out. i really am interested in the script you have created. Can you bust out a version that will work locally?

And what do you mean by
although it does rely a bit on how you vary in the usage:
thank you.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The version I posted is intended for the command line.

As for "how you vary in the usage," what I mean is how you write the short tags. There are multiple ways to write the short tags, some are far more common than others. Examples of the common ways:

Code: Select all

<? ... ?>
<?= ... ?>
The code I posted is intended for the common ways.
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

feyd wrote:short_tags which is a php.ini directive.

It is best to alter all of your files to add the correct tags however. This can be done via a script or command-line fairly easily, although it does rely a bit on how you vary in the usage:

Code: Select all

[feyd@home]> php -r "foreach(glob('*.php') as $file) { file_put_contents($file, preg_replace(array('#<\?\s*=#s', '#<\?(\s*)#s'), array('<?php echo ', '<?php$1'), file_get_contents($file))); }"
It may need a little tweaking depending on if I wrote it right (did it from memory) and based on how your write your code most often, but should work for most instances. Run that in each directory you have scripts in that use short tags.
interesting script ! I have been using search and replace tool to do that till now like regexxer in linux.

Thank you for the simple mechanism feyd !

Cheers,
Dibyendra
Post Reply