tkm Posted January 4, 2008 Share Posted January 4, 2008 Hello, Can anyone pls tell me what is "<<<" used for in PHP? The code I have with me uses "<<<" to assign a javascript block to a php variable. I think it is a very novice question but a reply would really help. I have searched in google but couldn't find anything related. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/84522--/ Share on other sites More sharing options...
wildteen88 Posted January 4, 2008 Share Posted January 4, 2008 DO you mean code which looks like this: echo <<<EOF big block of output here EOF; If so that is called a HEREDOC statement. Quote Link to comment https://forums.phpfreaks.com/topic/84522--/#findComment-430618 Share on other sites More sharing options...
p2grace Posted January 4, 2008 Share Posted January 4, 2008 You use it to create strings with many quotes without having to escape each quote. Example: echo <<<HTML <a href="test.php">I'm using multiple "quotations" with $variables inside. HTML; Quote Link to comment https://forums.phpfreaks.com/topic/84522--/#findComment-430620 Share on other sites More sharing options...
awpti Posted January 4, 2008 Share Posted January 4, 2008 NOTE: HEREDOC is very inefficient. Avoid using it. Quote Link to comment https://forums.phpfreaks.com/topic/84522--/#findComment-430634 Share on other sites More sharing options...
sKunKbad Posted January 4, 2008 Share Posted January 4, 2008 NOTE: HEREDOC is very inefficient. Avoid using it. I'm not saying your wrong, but why do you say that? Quote Link to comment https://forums.phpfreaks.com/topic/84522--/#findComment-430826 Share on other sites More sharing options...
mrdamien Posted January 5, 2008 Share Posted January 5, 2008 NOTE: HEREDOC is very inefficient. Avoid using it. I'm not saying your wrong, but why do you say that? Yeah, explain please. There is a benchmark on the HEREDOC. The numbers point to heredoc being faster with string replacement. test_double_quote_replace : 147ms test_eot_replace : 130ms Quote Link to comment https://forums.phpfreaks.com/topic/84522--/#findComment-430834 Share on other sites More sharing options...
p2grace Posted January 5, 2008 Share Posted January 5, 2008 I agree, I use HEREDOC all the time and I've never had a problem with it. Quote Link to comment https://forums.phpfreaks.com/topic/84522--/#findComment-430837 Share on other sites More sharing options...
awpti Posted January 5, 2008 Share Posted January 5, 2008 They are more intensive for PHP to parse than single or double-quoted strings, resulting in slower code execution and increased memory usage (memory usage part isn't always noticable). http://www.awpti.org/test_heredoc/ - >Result: ~0.178 -> 0.233 http://www.awpti.org/test_heredoc/double_quote/ - >Result: ~ 0.133 -> 0.145 Take into consideration that this test was built using the CodeIgniter Benchmark class/Framework. The actual times on this will be lower, but double_quote is still faster. Load the page and scroll to the bottom. And the code.. <?php class Test_heredoc extends Controller { function Test_heredoc() { parent::Controller(); } function index() { $this->benchmark->mark('code_start'); $tests = 10000; for($i=0;$i<$tests;++$i) { $string = 'blah-'.$i; echo <<<EOF This is a test $string. This should run 10000 times.<br /> EOF; } $this->benchmark->mark('code_end'); echo 'Time taken: '.$this->benchmark->elapsed_time('code_start', 'code_end').'<br />'; $this->view->load('test/mem'); } function double_quote() { $this->benchmark->mark('code_start'); $tests = 10000; for($i=0;$i<$tests;++$i) { $string = 'blah-'.$i; echo "This is a string with $string in it.<br />"; } $this->benchmark->mark('code_end'); echo 'Time taken: '.$this->benchmark->elapsed_time('code_start', 'code_end').'<br />'; $this->view->load('test/mem'); } } ?> In retrospect, I should have named this test_perf Quote Link to comment https://forums.phpfreaks.com/topic/84522--/#findComment-430853 Share on other sites More sharing options...
sKunKbad Posted January 5, 2008 Share Posted January 5, 2008 When I ran your tests just now, there was a four tenths of a second difference, and memory usage was the exact same. I think it is VERY unlikely that the difference is great enough to effect the user's experience of any of my pages. While I do agree there your tests prove that heredoc is slower than double quotes when looped 10000 times, in the case of normal usage the difference is probably indistinguishable. Let's not quibble over such a minute amount of time! Saying , "heredoc is very inefficient. Avoid using it.", is a bit of an exaggeration. Quote Link to comment https://forums.phpfreaks.com/topic/84522--/#findComment-430862 Share on other sites More sharing options...
mrdamien Posted January 5, 2008 Share Posted January 5, 2008 Interesting result but I couldn't help notice how the HEREDOC test uses a longer string. Could you do the same test but with both strings exactly the same? echo <<<EOF This is a test . This should run 10000 times.<br /> EOF; $string = 'blah-'.$i; echo "This is a string with in it.<br />"; Quote Link to comment https://forums.phpfreaks.com/topic/84522--/#findComment-430863 Share on other sites More sharing options...
awpti Posted January 5, 2008 Share Posted January 5, 2008 I shrunk the string, now they're both about the same. I didn't think about it earlier, but these results may be poisoned by the fact my hosting server is a Virtual instance among a few others. They both swing pretty big in the results - ranging from 0.3x to 0.9x+ I'll actually be shifting off to a dedicated server soon - be able to do a more reliable test then. As far as the difference being minor? Any savings is a saving. I'll also assume you ment 4 hundredths and not 4 tenths. 4 tenths would be a significant savings (and an odd result ). It may be a -slight- exaggeration to say 'very' on short HEREDOC's.. start pumping out serious content w/ variables using HEREDOC and you'll feel the hurt when the site gets active. I had to deal with a site that did such a thing and it was AWFUL. The performance was horrible and the memory usage difference was visible (still insignificant compared to the processing time). Every page was, pretty much, a HEREDOC with about 30-40 variables being dumped to it, with breaks out of the HEREDOC to take care of 'for' loops. Start running load tests. You'll see load times/processing times increase exponentially. Every little tweak matters when your site is live and high-traffic. That .04 can be the difference between crushing your server's CPU or not during a spike. Quote Link to comment https://forums.phpfreaks.com/topic/84522--/#findComment-430881 Share on other sites More sharing options...
hitman6003 Posted January 5, 2008 Share Posted January 5, 2008 For comparison, here is the code and results I used / got: function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } $heredoc_times = array(); $echo_times = array(); for ($i = 0; $i < 10000; $i++) { $start = microtime_float(); echo <<<LINE test test test $start test test test LINE; $heredoc_times[] = (microtime_float() - $start); } for ($i = 0; $i < 10000; $i++) { $start = microtime_float(); echo " test test test $start test test test "; $echo_times[] = (microtime_float() - $start); } echo '<pre> HEREDOC: Total: ' . array_sum($heredoc_times) . ' Average: ' . (array_sum($heredoc_times) / count($heredoc_times)) . ' echo: Total: ' . array_sum($echo_times) . ' Average: ' . (array_sum($echo_times) / count($echo_times)); HEREDOC: Total: 0.210633277893 Average: 2.10633277893E-5 echo: Total: 0.221266508102 Average: 2.21266508102E-5 I'm using a Core2 Duo Laptop with 2 GB RAM running the latest version of XAMPP (PHP 5.2.5, Apache 2.2.6). Quote Link to comment https://forums.phpfreaks.com/topic/84522--/#findComment-430915 Share on other sites More sharing options...
awpti Posted January 5, 2008 Share Posted January 5, 2008 Seems like everyone is getting different results. At the console (php test.php) HEREDOC: Total: 0.28366875648499 Average: 2.8366875648499E-5 echo: Total: 0.22118401527405 Average: 2.2118401527405E-5 Via apache (2.2.6 w/ php 5.2.5): HEREDOC: Total: 0.28366875648499 Average: 2.8366875648499E-5 echo: Total: 0.22118401527405 Average: 2.2118401527405E-5 ??? Quote Link to comment https://forums.phpfreaks.com/topic/84522--/#findComment-430935 Share on other sites More sharing options...
tkm Posted January 5, 2008 Author Share Posted January 5, 2008 Hello Everyone, Thank you. Great stuff. I wasn't expecting such quick response...and yes the code I am working on is using "<<<" as "Heredoc". Thank you guys again....love php freaks forum... Quote Link to comment https://forums.phpfreaks.com/topic/84522--/#findComment-431318 Share on other sites More sharing options...
mrdamien Posted January 5, 2008 Share Posted January 5, 2008 1.5ghz cpu apache 2.2.6 php 4 HEREDOC: Total: 0.538983345032 Average: 5.38983345032E-5 echo: Total: 0.798581123352 Average: 7.98581123352E-5 Quote Link to comment https://forums.phpfreaks.com/topic/84522--/#findComment-431458 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.