I'm currently using PHP to dynamically create a javascript which will be echoed on the page.

Sample Code:

$JS .= '<script>';

if($condition == true) {
    $JS .= 'alert("Yo its true omg!");
}

$JS .= "</script>";

As you can see, this will eventually get messy with ll the ' quotes and escaping of single quotes within the double quotes...

Is there a better way to do this?

share|improve this question
If I may ask, why do this? – Codemonkey Jul 5 '11 at 14:38
2  
Yes, a better way is to NOT do this. – Gregg Jul 5 '11 at 14:39
1  
I'm doing this because I need to create X chunks of JS code for the X results I retrive from the mySQL database. Each result will create its own say div id like <div id="marker_1">, <div id="marker_2"> etc... – Nyxynyxx Jul 5 '11 at 15:13

4 Answers

up vote 4 down vote accepted

You want the heredoc syntax!

if($condition)
    $statement = <<<JS
        alert("Wohoo!");
JS;
else $statement = "";

$javascript = <<<JS
    <script>
        $statement
    </script>
JS;

To handle conditional statements inside heredoc strings, simply do the condition logic beforehand and insert empty or filled strings inside the heredoc string. You can insert variables into heredoc strings the same way you do normal strings.

If you think heredoc strings are a hassle to define, I agree with you. Unfortunately, as far as I know, it's the only way to escape the even greater quote escaping hassle (No pun intended).

share|improve this answer
1  
Heredoc requires that the first line after the opening tag is at the very left edge of the next line – Alex Jul 5 '11 at 14:41
Is it possible to use PHP's if() conditional statements, or any other PHP code? – Nyxynyxx Jul 5 '11 at 15:14
I'll update my answer with the way I usually deal with such. – Codemonkey Jul 5 '11 at 16:12
Alex: No it doesn't. All it requires is that the closing tag is alone on a line with no whitespace, and that the opening tag is at the far right of its line. – Codemonkey Jul 5 '11 at 16:23
Also note that you might need to json_encode() or addslashes() if you are exporting values from PHP to Javascript. – todofixthis Jul 6 '11 at 1:36

You would be better to create the dynamic javascript file still as a separate file with the PHP extension then just before outputting the javascript set the header content type to text/javascript

$js = <<<JS
// Your JS here
JS;

header("Content-type: text/javascript");
echo $js;
exit();
share|improve this answer

Create a PHP file, but use a content header to make it 'look' like a JS file to the Browser. Because the file extension is .php Apache (or whatever other web server you're using) will still parse it, but the browser will see it as a JS file and run the contents properly. I use this frequently for CSS files because it allows me to declare variables and take advantage of loops and other more powerful control concepts lacking from CSS.

share|improve this answer
echo <<<EOF
 <script>
      alert("Yo it's true omg!");
 </script>

EOF;
share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.