tags:

views:

1257

answers:

10

Hi, 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.

+9  A: 

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

// PHP uses single quotes, JavaScript uses double quotes
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
A: 

You want to do this:

echo "document.write('Hello World!')";

I would suggest to read an article or book about PHP to get the Syntax right...

Malax
A: 
<?php

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

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
+1  A: 

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
+4  A: 

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.

Johan
A: 

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
+3  A: 

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
+1 for using HEREDOC, which is a much better way to handle a large code block.
Darren Newton
+1. Agreed, use a heredoc if you are including large code blocks.
Grant Wagner
+1  A: 

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

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ SEE CODE BELOW ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

<?php
  // prepare JavaScript variable
$greeting = 'Hello World!';
?>
<html>
    <head>
     <title>Javascript Embedding Example</title>
    </head>
<body>

    <div id="helloContainer"><div>

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

</body>
</html>
wilmoore
A: 

You want to do this:

<html>
<body>
<?php

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

?>
</body>
</html>
Maury