Re: echo or print

From: Date: Sat, 22 Nov 2003 04:20:47 +0000
Subject: Re: echo or print
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-170654@lists.php.net to get a copy of this message
Rasmus Lerdorf "There is a difference between the two, but speed-wise it should be irrelevant which one you use. print() behaves like a function in that you can do: $ret = print "Hello World"; And $ret will be 1 That means that print can be used as part of a more complex expression where echo cannot. print is also part of the precedence table which it needs to be if it is to be used within a complex expression. It is just about at the bottom of the precendence list though. Only "," AND, OR and XOR are lower. echo is marginally faster since it doesn't set a return value if you really want to get down to the nitty gritty. If the grammar is: echo expression [, expression[, expression] ... ] Then echo ( expression, expression ) is not valid. ( expression ) reduces to just an expression so this would be valid: echo ("howdy"),("partner"); but you would simply write this as: echo "howdy","partner"; if you wanted to use two expression. Putting the brackets in there serves no purpose since there is no operator precendence issue with a single expression like that." -- Jon Kriek www.phpfreaks.com Wouter Van Vliet wrote:
Chris W. Parker wrote:
Wouter van Vliet <mailto:wouter@springt.nl> on Friday, November 21, 2003 10:55 AM said:
Point is, which of the inline printing style is preferred by you guyes. I tend to use <?=$Var?> a lot, since it reads easier but get into struggles with myself when I do that multiple times in a row.
Because of this I usually do the following: echo "<p>here is some text with a $variable in it.<br/>\n" ."And this is another like of text with a $variable1 in it.<br/>\n" ."And so on...<br/>\n" ."And so forth.</p>\n"; I also prefer <?= $variable ?> to <?php echo $variable; ?> except that for the sake of cross-system compatibility* I now choose to do <?php echo $variable; ?>. Chris. * What I mean by that is if I give my code to someone else I want it to work with as few changes as possible. Some php installs don't have <? ?> turned on (short tags?).
Well, there is an eye opener. I always thought that the <?=$Var?> printing style was not influenced by short_open_tag, but now I did a test to be sure about it and it turned out it does.. <quick test>
      1 <?php
      2 echo 'ini setting short_open_tag: '.ini_get('short_open_tag');
      3 ?>
      4
      5 Long open tags: <?php print 'OK'; ?>
      6 Short open tags <? print 'OK'; ?>
      7 Short print style <?='OK'?>
<output short_open_tags="On"> ini setting short_open_tag: 1 Long open tags: OK Short open tags OK Short print style OK </output> <output short_open_tags="Off"> ini setting short_open_tag: Long open tags: OK Short open tags <? print 'OK'; ?> Short print style <?='OK'?> </output> </quick_test> Thanks! Wouter


« previous php.general (#170654) next »