JustinK101 Posted September 16, 2009 Share Posted September 16, 2009 Is there a way to echo a bunch of html without being inside of php? For example I have the following: function load_analytics($key) { echo '<script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E")); </script> <script type="text/javascript"> var pageTracker = _gat._getTracker("' . $key . '"); pageTracker._trackPageview(); </script>'; } Would prefer to do this without putting all my html code inside of an echo. Can I do << or something along those lines? Quote Link to comment https://forums.phpfreaks.com/topic/174417-way-to-echo-html-without-being-in-php-tags/ Share on other sites More sharing options...
dennismonsewicz Posted September 16, 2009 Share Posted September 16, 2009 change function load_analytics($key) { echo '<script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E")); </script> <script type="text/javascript"> var pageTracker = _gat._getTracker("' . $key . '"); pageTracker._trackPageview(); </script>'; } to this <?php function load_analytics($key) { ?> <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E")); </script> <script type="text/javascript"> var pageTracker = _gat._getTracker("' . $key . '"); pageTracker._trackPageview(); </script> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/174417-way-to-echo-html-without-being-in-php-tags/#findComment-919316 Share on other sites More sharing options...
Zane Posted September 16, 2009 Share Posted September 16, 2009 Heredocs http://www.phpf1.com/tutorial/php-heredoc-syntax.html Lengthier Example http://onlamp.com/pub/a/php/2003/04/10/php_heredocs.html Quote Link to comment https://forums.phpfreaks.com/topic/174417-way-to-echo-html-without-being-in-php-tags/#findComment-919318 Share on other sites More sharing options...
JustinK101 Posted September 16, 2009 Author Share Posted September 16, 2009 Appears heredocs is the way to go. <?php function load_analytics($key) { echo <<<EOT <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E")); </script> <script type="text/javascript"> var pageTracker = _gat._getTracker("' . $key . '"); pageTracker._trackPageview(); </script> EOT; ?> Quote Link to comment https://forums.phpfreaks.com/topic/174417-way-to-echo-html-without-being-in-php-tags/#findComment-919319 Share on other sites More sharing options...
JustinK101 Posted September 16, 2009 Author Share Posted September 16, 2009 Humm, appears it does not work though with static class properties? The code inst replacing GoogleAnalytics::$key simple writing: GoogleAnalytics:: Ideas? class GoogleAnalytics { /** * @staticvar string */ private static $key = "UA-3456468-9"; public static function load() { echo <<<EOD <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E")); </script> <script type="text/javascript"> var pageTracker = _gat._getTracker("GoogleAnalytics::$key"); pageTracker._trackPageview(); </script> EOD; } } Output is: var pageTracker = _gat._getTracker("GoogleAnalytics::"); Quote Link to comment https://forums.phpfreaks.com/topic/174417-way-to-echo-html-without-being-in-php-tags/#findComment-919321 Share on other sites More sharing options...
Zane Posted September 16, 2009 Share Posted September 16, 2009 try either echo or [code]echo EDIT: no...nix that idea Quote Link to comment https://forums.phpfreaks.com/topic/174417-way-to-echo-html-without-being-in-php-tags/#findComment-919326 Share on other sites More sharing options...
JustinK101 Posted September 16, 2009 Author Share Posted September 16, 2009 So this method works. Is this method frowned upon as bad practice? I couldn't get heredoc working. <?php class GoogleAnalytics { /** * @staticvar string */ private static $key = "UA-3456468-9"; public static function load() { ?> <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E")); </script> <script type="text/javascript"> var pageTracker = _gat._getTracker("<?= GoogleAnalytics::$key ?>"); pageTracker._trackPageview(); </script> <?php } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/174417-way-to-echo-html-without-being-in-php-tags/#findComment-919328 Share on other sites More sharing options...
Zane Posted September 16, 2009 Share Posted September 16, 2009 http://bugs.php.net/bug.php?id=41776 Update: It seems like class static variables are not parsed inside any quoted string. And because heredoc is practically a quoted string, they are not parsed inside heredoc too!!! Quote Link to comment https://forums.phpfreaks.com/topic/174417-way-to-echo-html-without-being-in-php-tags/#findComment-919329 Share on other sites More sharing options...
Zane Posted September 16, 2009 Share Posted September 16, 2009 I know I'm not coding this but it seems to me that you're in the GoogleAnalytics Class trying to access one of it's(AKA this) static properties GoogleAnalytics::$key or $this->key Why can't you just use $this->key?....along with the heredoc or have you tried that already Quote Link to comment https://forums.phpfreaks.com/topic/174417-way-to-echo-html-without-being-in-php-tags/#findComment-919331 Share on other sites More sharing options...
JustinK101 Posted September 16, 2009 Author Share Posted September 16, 2009 Zanus: Thanks for the replies. Yeah I tried it, but accessing static class properties in heredoc does not work. As you said in your previous post. Am I missing something though? Quote Link to comment https://forums.phpfreaks.com/topic/174417-way-to-echo-html-without-being-in-php-tags/#findComment-919336 Share on other sites More sharing options...
Zane Posted September 16, 2009 Share Posted September 16, 2009 nah...I guess so long as it works for you then...it works.. you may want to put a big comment block in there to remind you of why you have to open and close the php tags..if not to inform someone else. just remember to mark as solved if you're done Quote Link to comment https://forums.phpfreaks.com/topic/174417-way-to-echo-html-without-being-in-php-tags/#findComment-919337 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.