TheFilmGod Posted July 18, 2009 Share Posted July 18, 2009 I just finished building the front end for a very complex application and I'm about to start the programming aspect of it. My question - what's the best way of echoing html? I usually just do echo '......................' but I heard there are more efficient ways? What about variables? HOw do you do it: 1) echo '..html...' . $variable; 2) echo '...html..'; echo $variable; Quote Link to comment https://forums.phpfreaks.com/topic/166392-echoing-html/ Share on other sites More sharing options...
Philip Posted July 18, 2009 Share Posted July 18, 2009 <?php echo 'html'.$var; echo 'html',$var; // I personally use this way. the tests I've run show its slightly faster because it is taking it is separate arguments instead of concatenate them echo "html {$var}"; // or if you have a lot of html to display: ?> <html stuff> <?php echo $var; //... Quote Link to comment https://forums.phpfreaks.com/topic/166392-echoing-html/#findComment-877434 Share on other sites More sharing options...
TheFilmGod Posted July 18, 2009 Author Share Posted July 18, 2009 Something I never understood is why are double quotes the ones that interpret variables? Taking into account html and attributes ... attribute="value"... it would make sense to make single quotes interpret variables. echo "html {$var}"; - I have never seen this before. Can someone tell me what this means? Quote Link to comment https://forums.phpfreaks.com/topic/166392-echoing-html/#findComment-877439 Share on other sites More sharing options...
Alex Posted July 18, 2009 Share Posted July 18, 2009 The literal for single quotes is "read as string". So everything inside of single quotes will read as a string. Quote Link to comment https://forums.phpfreaks.com/topic/166392-echoing-html/#findComment-877443 Share on other sites More sharing options...
lostprophetpunk Posted July 18, 2009 Share Posted July 18, 2009 Something I never understood is why are double quotes the ones that interpret variables? Taking into account html and attributes ... attribute="value"... it would make sense to make single quotes interpret variables. But you can always echo the " by adding a slash like \" or you could use the ' for the attributes, as it works just as well such as echo "<a href='#'>Test</a>"; Quote Link to comment https://forums.phpfreaks.com/topic/166392-echoing-html/#findComment-877510 Share on other sites More sharing options...
GingerRobot Posted July 18, 2009 Share Posted July 18, 2009 Something I never understood is why are double quotes the ones that interpret variables? Taking into account html and attributes ... attribute="value"... it would make sense to make single quotes interpret variables. echo "html {$var}"; - I have never seen this before. Can someone tell me what this means? The use of the braces is simply to 'help' the PHP engine. In this case, they're not required as it can work out that you want to happen. But if you had this, for example: <?php class foo{ private $var = 'some value'; function __construct(){ echo "html {$this->getVar()}" . "\n"; } function getVar(){ return $this->var; } } $foo = new foo(); Then it won't work without the braces -- you'll get "html ()" as the output. Quote Link to comment https://forums.phpfreaks.com/topic/166392-echoing-html/#findComment-877513 Share on other sites More sharing options...
Daniel0 Posted July 18, 2009 Share Posted July 18, 2009 echo "html {$var}"; - I have never seen this before. Can someone tell me what this means? Check the manual for the string data type. Quote Link to comment https://forums.phpfreaks.com/topic/166392-echoing-html/#findComment-877515 Share on other sites More sharing options...
Daniel0 Posted July 18, 2009 Share Posted July 18, 2009 echo 'html',$var; // I personally use this way. the tests I've run show its slightly faster because it is taking it is separate arguments instead of concatenate them That is incorrect. The way the engine handles multiple arguments to echo() means concatenation is faster. See: http://pastie.org/523020 Also see: http://groups.google.com/group/make-the-web-faster/browse_thread/thread/ddfbe82dd80408cc These kinds of micro optimizations are mostly useless anyway. Don't optimize your code before you've profiled it to find out where the bottlenecks are. Otherwise you just risk ending up wasting time on premature optimization. Quote Link to comment https://forums.phpfreaks.com/topic/166392-echoing-html/#findComment-877518 Share on other sites More sharing options...
TheFilmGod Posted July 18, 2009 Author Share Posted July 18, 2009 Are the braces necessary for multidimensional arrays? Like so: {$big_array[small_array[0]]} or will the php engine know what you mean? Which technique do the gurus on this forum use to echo html? Do you just do the single quote + concatenation type? Quote Link to comment https://forums.phpfreaks.com/topic/166392-echoing-html/#findComment-877635 Share on other sites More sharing options...
MadTechie Posted July 19, 2009 Share Posted July 19, 2009 Are the braces necessary for multidimensional arrays? Like so: {$big_array[small_array[0]]} or will the php engine know what you mean? okay think if it this way Your going to echo out a variable ($var) and some StaticText with NO space between them.. echo "$varStaticText"; and you get Nothing because their is no variable called $varStaticText If a dollar sign ($) is encountered' date=' the parser will greedily take as many tokens as possible to form a valid variable name. Enclose the variable name in curly braces to explicitly specify the end of the name. [/quote'] so you have 2 choices echo $var."StaticText"; or echo "{$var}StaticText"; Which technique do the gurus on this forum use to echo html? Do you just do the single quote + concatenation type? The forum was written by SMF.. but personally I use what ever suites at the time, by default when echoing html I'll use double quotes, but it really does depend on what I'm doing Quote Link to comment https://forums.phpfreaks.com/topic/166392-echoing-html/#findComment-877777 Share on other sites More sharing options...
Andy-H Posted July 19, 2009 Share Posted July 19, 2009 If I am echoing out plain HTML without any PHP variables I usually jump out of PHP, if I'm using variables in a small amount of HTML I use single quotes and concatenation. If I am using alot of HTML with quoted elements I like to use a heredoc which uses the C++ cout syntax and you can use any quotes or variables you need, $str = <<<EOD Example of string spanning multiple lines using heredoc syntax. EOD; From http://uk2.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc Quote Link to comment https://forums.phpfreaks.com/topic/166392-echoing-html/#findComment-877792 Share on other sites More sharing options...
Philip Posted July 19, 2009 Share Posted July 19, 2009 That is incorrect. The way the engine handles multiple arguments to echo() means concatenation is faster. See: http://pastie.org/523020 Interesting, when I tested it a few months ago on my server multiple arguments was faster, but like you said, not by enough to matter. Quote Link to comment https://forums.phpfreaks.com/topic/166392-echoing-html/#findComment-877801 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.