0

I have some code that doesn't work unless I change the single quotes to double quotes around the numbers (600, 120, 99999999).

I'm using the below as the original code that won't work -

"Parse error: syntax error, unexpected T_STRING"

I am trying to avoid changing ' to " (which works) because then I'm not using the original ad code. Can someone let me know what the code should look like to make it work with single quotations in there?

echo '<script id="mNCC" language="javascript">  medianet_width='600';  medianet_height= '120';  medianet_crid='99999999';  </script>  <script id="mNSC" src="http://contextual.media.net/nmedianet.js?cid=1234567" language="javascript"></script>';
Prashant Srivastav
  • 1,723
  • 17
  • 28
  • Escape them or use HEREDOC – Rizier123 Jul 27 '15 at 21:05
  • You don't have to escape anything. Your JS code contains integers anyway, just remove single quotes and echo the thingy. Now you learned the best method to escape stuff - get to the point where you don't need to escape things. – N.B. Jul 27 '15 at 21:11
  • @N.B. It's recommended to put quotes around all attribute values. – Barmar Jul 27 '15 at 21:12
  • @Barmar - his problems are **variables**, they are enclosed with single quotes. They are not needed (the quotes, not variables). Why are you mentioning that attributes need to be quoted is beyond me really. – N.B. Jul 27 '15 at 21:14

2 Answers2

1

You will have to 'escape' your single quotes, otherwise they will terminate your echo string.

echo '<script id="mNCC" language="javascript">  medianet_width=\'600\';  medianet_height= \'120\';  medianet_crid=\'99999999\';  </script>  <script id="mNSC" src="http://contextual.media.net/nmedianet.js?cid=1234567" language="javascript"></script>';
MaggsWeb
  • 3,018
  • 1
  • 13
  • 23
1

Ignore the JS. You're writing PHP. If you have a string that uses the SAME quotes that the string itself is enclosed with, then the quotes have to be escaped:

$foo = "Miles O'Brien"; // perfectly ok
$foo = 'Miles O'Brien'; // bad - string now ends at O
$foo = 'Miles O\'Brien'; // perfectly ok
Marc B
  • 356,200
  • 43
  • 426
  • 500