24

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.

Blixt
  • 49,547
  • 13
  • 120
  • 153
  • 1
    Oh nice, someone "enhanced" the original code in the question by editing it... Now my answer looks dump. :P – Malax Jul 22 '09 at 09:41

12 Answers12

61

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 "'";
Ionuț G. Stan
  • 176,118
  • 18
  • 189
  • 202
  • 9
    ok time to give this answer a correct! vote. Come on @krishna, this is worthy. This guys took time out for you. I think 3 years is long enough to work this one out mate. – 3Dom Sep 25 '12 at 12:50
  • 3
    May I suggest a change from `// PHP uses single quotes, JavaScript uses double quotes` to `// Here, we use single quotes for PHP and double quotes for JavaScript`? That way it's more clear that those aren't rules about PHP and JavaScript – Jasper Nov 13 '12 at 13:09
  • 1
    I found this answer really helpful. Thanks a lot! – DragonKyn Jul 13 '17 at 01:48
35

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.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
13

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.

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Johan
  • 18,814
  • 30
  • 70
  • 88
6

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;

?>
Turnor
  • 1,836
  • 12
  • 14
2

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.

Randell
  • 6,112
  • 6
  • 45
  • 70
2

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">
Greg
  • 316,276
  • 54
  • 369
  • 333
1

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>
    

Alexander Holsgrove
  • 1,795
  • 3
  • 25
  • 54
Wil Moore III
  • 6,968
  • 3
  • 36
  • 49
0

You want to do this:

<html>
<body>
<?php

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

?>
</body>
</html>
Maury
  • 1
0

instead you could easily do it this way :

<html>
<body>
<script type="text/javascript">
<?php
  $myVar = "hello";
?>
   document.write("<?php echo $myVar ?>");
</script>
</body>
Majid Abdolhosseini
  • 2,191
  • 4
  • 31
  • 59
0

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>
tawsif torabi
  • 713
  • 7
  • 14
-2
<?php

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

Try This:

<html>
<body>
<?php

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

?>
</body>
</html>
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
Adam
  • 9
  • 2
    This doesn't work for the same reason as the code in the original question didn't (did you even change anything on the code?) – Jasper Nov 13 '12 at 13:12
  • 1
    @Developer Please do not correct programming errors by editing. Rather, leave a comment to notify the author about the deficiencies of their answer. – Mathias Müller Mar 04 '14 at 09:50
  • ok cool. sorry abt that.wanted to save some one from this code by correcting it :( – Developer Mar 04 '14 at 09:53