Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have created the following php script with javascript using a text editor and to run the php file I used XAMPP. But when I run the file I got an error. I saved the PHP file with the .php extension. At the below I am inserting the source code and the error message please try to solve my problem!

XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>PHP With JavasCript</title>
</head>
<body>
    <?php

        $a = 15;
        
        if($a > 10){
            echo "<script type="text/javascript">
            
                    document.write("OK!");            
            
                </script>";
        }
    
    
    ?>
</body>
</html>


Parse error: syntax error, unexpected 'text' (T_STRING), expecting ',' or ';' in C:\xampp\htdocs\HND_WEB\PHP\PHP_with_JavaScript.php on line 13
Posted
Comments
[no name] 23-Sep-12 5:21am    
Count the number of quotes on that line....
[no name] 23-Sep-12 5:21am    
and then add a ;
Chiranthaka Sampath 23-Sep-12 5:38am    
I have change the source code as at the below but gives the same result

<pre>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP With JavasCript</title>
</head>
<body>
10){
echo <script language = "JavaScript" type="text/javascript">;

document.write("OK!");

</script>;
}


?>
</body>
</html>


</pre

You have to insert escape character (\) before each double quote that is inside a double-quoted string.
PHP
<?php
    $a = 15;
    if($a > 10){
        echo "<script type=\"text/javascript\">
                document.write(\"OK!\");
            </script>";
    }
?>
 
Share this answer
 
Comments
Chiranthaka Sampath 23-Sep-12 6:31am    
Ok dude I owe you! The problem was solved many thanx!
Another way to solve it, without adding backslashes is "heredoc" syntax:

PHP
<?php
    $a = 15;
    if($a > 10){
        echo <<<JAVASCRIPT
<script type="text/javascript">
  document.write("OK!");
</script>
JAVASCRIPT;
    }
?></javascript>
 
Share this answer
 
v2
Huseyin Atasoy's escaping characters methods works well.

Or you could simply put single quotes(') around the echoing string like this.

PHP
echo '<script type="text/javascript">
          document.write("OK!");        
      </script>';
?>


That works too.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900