anevins Posted April 2, 2011 Share Posted April 2, 2011 Hi there, I'm trying to implement javascript by echoing PHP but I keep getting a syntax error. Here's the code: echo '<script> function word_count(review, doc) { review = review.replace(/^\s*|\s*$/g,''); //removes whitespace from front and end var count_array = review.split(" "); doc.value = count_array.length; } </script> '; I get this error: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in ... line 33 line 33: '; Can anyone figure out the syntax error? Link to comment https://forums.phpfreaks.com/topic/232512-syntax-error/ Share on other sites More sharing options...
SamT_ Posted April 2, 2011 Share Posted April 2, 2011 Try the Herdoc syntax: <?php echo <<<EOT <script> function word_count(review, doc) { review = review.replace(/^\s*|\s*$/g,''); //removes whitespace from front and end var count_array = review.split(" "); doc.value = count_array.length; } </script> EOT; My guess is it doesn't like how you have delimiters to the PHP string in your javascript replace method. Link to comment https://forums.phpfreaks.com/topic/232512-syntax-error/#findComment-1195958 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 solved Thank you Sam. Link to comment https://forums.phpfreaks.com/topic/232512-syntax-error/#findComment-1195961 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.