Jump to content

Syntax Error


anevins

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.