Jump to content

Recommended Posts

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?

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      }  ?>

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;
?>

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::");

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
	}
}
?>

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!!!

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.