= / echo

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
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

= / echo

Post by mrvanjohnson »

Is this considered "sloppy code"

Code: Select all

<?=$variable?>
Compared to

Code: Select all

<?php echo $variable ?>
Working on a project I think I might release in Sourceforge and I just want to have the most compatiable, cleanest code. Any opinions?
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

I would personally consider <?php echo $variable; ?> to be the neatest and easiest to read and understand.
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

Both work same. Maybe you need ';' in the second command, I'm not sure here. But I use the first one very often.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Hmm. i never heard of that first one
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

The first one is only supported if 'short open tags' is enabled in your php config.

Personally I always use the later as you never know what server config you are going to come across.

I wouldn't consider it sloppy coding but I would consider it to be less portable than your comparison.
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Post by mrvanjohnson »

Thanks everyone. Especially redmonkey, that was exactly the answer I was looking for.

Just FYI magicrobotmonkey, <?= is PHP short hand for <?php echo :-)
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Yea I just checked it out in the manual. Guess its a good way to save a few keystrokes...
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

I use the 1st one but what redmonkey says is right.
I think you should use the 2nd, if you release it on SourceForge.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

i only use the first one when just echoing small blocks, like an small echo-statement in HTML
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

It is recommended not to use use the shorthand way if you're dealing with XML since it uses the same "<?"
Post Reply