Newbie question: output shorthand

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
Niveras
Forum Newbie
Posts: 2
Joined: Thu Jan 11, 2007 1:08 pm

Newbie question: output shorthand

Post by Niveras »

Good day.

Is there a simplified method of outputting information from a PHP variable, without resorting to <?php echo ?> or <?php print() ?>? I am basically looking for something akin to <%= %> from ASP.

I recently decided to learn PHP and MySQL. I am familiar with most of ASP's conventions (at least on a simplistic level), but although ASP and PHP have general similarities, some of the logic and format is quite different. I know of the obvious resources for finding functions and whathaveyou (php.net's reference manual), but I wasn't sure how to search for the question above.

My apologies if this is rather more basic than what you normally on the forum. Additionally, if you know of a good guide to outline basic differences between ASP and PHP I would be most gracious. My thanks for your time.

- Niveras
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

How about <?=$var ?>. I believe not all PHP configurations support this type of shorthand....
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

In fact, php supports asp tags as well (though not by default, you have to turn it on). So <%=$somevar%> could potentially work!
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Short-tags (<?) are considered bad by practice by some because of the possible confusion with XML:

Code: Select all

<?xml version="1.0"?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

If php accepts <?, <?= or even <% is controlled by the config parameters short_open_tag and asp_tags
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Newbie question: output shorthand

Post by onion2k »

Niveras wrote:Is there a simplified method of outputting information from a PHP variable, without resorting to <?php echo ?> or <?php print() ?>? I am basically looking for something akin to <%= %> from ASP.

I recently decided to learn PHP and MySQL. I am familiar with most of ASP's conventions (at least on a simplistic level), but although ASP and PHP have general similarities, some of the logic and format is quite different. I know of the obvious resources for finding functions and whathaveyou (php.net's reference manual), but I wasn't sure how to search for the question above.
Years ago I switched from ASP to PHP, and my advice would be that you shouldn't try to write ASP code in PHP's syntax. Try to forget as much of your ASP knowledge as possible while you're writing PHP. While PHP lets you hack in shorthand outputs like <?=$var?> doing so is a really bad idea. It's a bad idea in ASP too. It's a clear indicator that you're mixing code and HTML more than you really should be.
Niveras
Forum Newbie
Posts: 2
Joined: Thu Jan 11, 2007 1:08 pm

Post by Niveras »

My thanks for all the replies. I think in the interest of conventions I'll just rely on using printf() or echo as the situation demands; might even force me to use a more efficient means since it won't permit me to be so lazy.

Apparently, on a more embarrasing note, my question was actually answered under the 'Basic Syntax' of PHP's manual. Perhaps I should take Vodka's signature to heart in the future~

- Niveras
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Considering short tags and asp tags are gone in future versions of PHP I would advise to stay as far away from them as possible. .. not to mention that they can be turned off on older and current versions thereby breaking code written using them.
Post Reply