[php] How to output JavaScript with PHP

I am new to PHP. I need to output the following JavaScript with PHP. This is my code:

<html>
<body>
<?php

echo "<script type="text/javascript">";
echo "document.write("Hello World!")";
echo "</script>";

?>
</body>
</html>

But it's showing the error:

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /var/www/html/workbench/person/script.php on line 4

Can anyone please help? I also need some simple tutorials on how to use PHP, HTML and JavaScript for an application.

This question is related to php

The answer is


You want to do this:

<html>
<body>
<?php

print '
<script type="text/javascript">
document.write("Hello World!")
</script>
';

?>
</body>
</html>

An easier way is to use the heredoc syntax of PHP. An example:

<?php

echo <<<EOF
<script type="text/javascript">
    document.write("Hello World!");
</script>
EOF;

?>

The following solution should work quite well for what you are trying to do.

  1. The JavaScript block is placed very late in the document so you don't have to worry about elements not existing.

  2. You are setting a PHP variable at the top of the script and outputting just the value of the variable within the JavaScript block.

    This way, you don't have to worry about escaping double-quotes or HEREDOCS (which is the recommended method if you REALLY must go there).

    Javascript Embedding Example

    <div id="helloContainer"><div>
    
    <script type="text/javascript">
        document.getElementById('helloContainer').innerHTML = '<?= $greeting; ?>';
    </script>
    


You should escape the JavaScript string delimiters inside the PHP string. You're using double quotes for both PHP and JavaScript strings. Try like this instead:

<html>
<body>
<?php

// Here, we use single quotes for PHP and double quotes for JavaScript
echo '<script type="text/javascript">';
echo 'document.write("Hello World!")';
echo '</script>';

?>
</body>
</html>

You have to escape quotes on both JavaScript and PHP when the string delimiter are the same as the quotes:

echo "\""; // escape is done using a backslash
echo '\'';

Same in JavaScript:

alert("\""); // escape is done using a backslash
alert(echo '\'');

But because it's very hard to read a string with such escape sequences, it is better to combine single with double quotes, as needed:

echo '"';
echo "'";

instead you could easily do it this way :

<html>
<body>
<script type="text/javascript">
<?php
  $myVar = "hello";
?>
   document.write("<?php echo $myVar ?>");
</script>
</body>

<?php

echo '<script type="text/javascript">document.write(\'Hello world\');</script>';
?>

Try This:

<html>
<body>
<?php

echo "<script type="text/javascript">";
echo "document.write("Hello World!");";
echo "</script>";

?>
</body>
</html>

You are using " instead of ' It is mixing up php syntax with javascript. PHP is going to print javascript with echo function, but it is taking the js codes as wrong php syntax. so try this,

<html>
<body>
<?php

echo "<script type='text/javascript'>";
echo "document.write('Hello World!')";
echo "</script>";

?>
</body>
</html>

The error you get if because you need to escape the quotes (like other answers said).

To avoid that, you can use an alternative syntax for you strings declarations, called "Heredoc"

With this syntax, you can declare a long string, even containing single-quotes and/or double-quotes, whithout having to escape thoses ; it will make your Javascript code easier to write, modify, and understand -- which is always a good thing.

As an example, your code could become :

$str = <<<MY_MARKER
<script type="text/javascript">
  document.write("Hello World!");
</script>
MY_MARKER;

echo $str;

Note that with Heredoc syntax (as with string delimited by double-quotes), variables are interpolated.


You need to escape your quotes.

You can do this:

echo "<script type=\"text/javascript\">";

or this:

echo "<script type='text/javascript'>";

or this:

echo '<script type="text/javascript">';

Or just stay out of php

<script type="text/javascript">

You need to escape the double quotes like this:

echo "<script type=\"text/javascript\">";
echo "document.write(\"Hello World!\")";
echo "</script>";

or use single quotes inside the double quotes instead, like this:

echo "<script type='text/javascript'>";
echo "document.write('Hello World!')";
echo "</script>";

or the other way around, like this:

echo '<script type="text/javascript">';
echo 'document.write("Hello World!")';
echo '</script>';

Also, checkout the PHP Manual for more info on Strings.

Also, why would you want to print JavaScript using PHP? I feel like there's something wrong with your design.


Another option is to do like this:

<html>
    <body>
    <?php
    //...php code...  
    ?>
    <script type="text/javascript">
        document.write("Hello World!");
    </script>
    <?php
    //....php code...
    ?>
    </body>
</html>

and if you want to use PHP inside your JavaScript, do like this:

<html>
    <body>
    <?php
        $text = "Hello World!";
    ?>
    <script type="text/javascript">
        document.write("<?php echo $text ?>");
    </script>
    <?php
    //....php code...
    ?>
    </body>
</html>

Hope this can help.