6

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?

2
  • 4
    Yes, a better way is to NOT do this. Commented Jul 5, 2011 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... Commented Jul 5, 2011 at 15:13

5 Answers 5

12

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).

Sign up to request clarification or add additional context in comments.

4 Comments

Heredoc requires that the first line after the opening tag is at the very left edge of the next line
Is it possible to use PHP's if() conditional statements, or any other PHP code?
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.
Also note that you might need to json_encode() or addslashes() if you are exporting values from PHP to Javascript.
5

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();

Comments

1

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.

Comments

1

If the static javscript code amount is more than your dinamically created javascript code amount, you can simply write javascript code as usual and then inject php where needed.

Example of a hypothetical index.php:

<?php

    $message = "Yo its true omg!";

?>

<html><head>

    <script>
        alert("<?php echo $message; ?>");
    </script>

</head><body>

    ...

</body></html>

Comments

0
echo <<<EOF
 <script>
      alert("Yo it's true omg!");
 </script>

EOF;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.