1.8. Interpolating Functions and Expressions Within Strings
print 'You have '.($_REQUEST['boys'] + $_REQUEST['girls']).' children.'; print "The word '$word' is ".strlen($word).' characters long.'; print 'You owe '.$amounts['payment'].' immediately'; print "My circle's diameter is ".$circle->getDiameter().' inches.';
print "I have $children children."; print "You owe $amounts[payment] immediately."; print "My circle's diameter is $circle->diameter inches.";
Direct interpolation or using string concatenation also works with heredocs. Interpolating with string concatenation in heredocs can look a little strange because the heredoc delimiter and the string concatenation operator have to be on separate lines:
print <<< END Right now, the time is END . strftime('%c') . <<< END but tomorrow it will be END . strftime('%c',time() + 86400);
Also, if you're interpolating with heredocs, make sure to include appropriate spacing for the whole string to appear properly. In the previous example, "Right now the time" has to include a trailing space, and "but tomorrow it will be" has to include leading and trailing spaces.
1.8.4. See Also
Copyright © 2003 O'Reilly & Associates. All rights reserved.