Page 1 of 1

Newbie question: output shorthand

Posted: Thu Jan 11, 2007 1:22 pm
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

Posted: Thu Jan 11, 2007 1:28 pm
by neophyte
How about <?=$var ?>. I believe not all PHP configurations support this type of shorthand....

Posted: Thu Jan 11, 2007 1:42 pm
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!

Posted: Thu Jan 11, 2007 1:50 pm
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"?>

Posted: Thu Jan 11, 2007 1:55 pm
by volka
If php accepts <?, <?= or even <% is controlled by the config parameters short_open_tag and asp_tags

Re: Newbie question: output shorthand

Posted: Thu Jan 11, 2007 2:35 pm
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.

Posted: Thu Jan 11, 2007 2:46 pm
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

Posted: Thu Jan 11, 2007 4:11 pm
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.