How to parse PHP variables in drupal_add_js in template.php I'm using drupal_add_js for some inline jQuery - I need to parse some php variables in the inline jQuery, what is the correct way to do it?

drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });',
  array('type' =&gt; 'inline', 'scope' =&gt; 'footer', 'weight' =&gt; 5)
);


I've tried this, and it DOES print the variable, but breaks the JS code in the process (splits the jquery code up and doesnt function) :

drupal_add_js('jQuery(document).ready(function () { alert(" '. print $site_name .' "); });',
  array('type' =&gt; 'inline', 'scope' =&gt; 'footer', 'weight' =&gt; 5)
);
Just remove the print function from your code:

drupal_add_js('jQuery(document).ready(function () { alert(" '. $site_name .' "); });',
  array('type' =&gt; 'inline', 'scope' =&gt; 'footer', 'weight' =&gt; 5)
);


Looking at the javascript Window alert() Method it has this syntax:


  alert(message)


And the message parameter has the type string

PHP's print function prints to the output buffer and returns an int. It has this definition:


  int print ( string $arg )


So your code prints the variable $site_name to the output buffer and returns an int, and your string for the alert is then an integer and you have printed to the output buffer which you did not want to do.