Embedding JavaScript

I’m new to PHP and I came across an example in a beginner’s book that I couldn’t get to work:

$MyArray[7] = 23;
echo '<script>alert(' . $MyArray[7] + 50 . ')</script>'

Now, I’m not sure if the Kindle browser app I’m using to read this book screwed up the formatting, but that’s exactly how it appeared. The output is supposed a JS alert box displaying the number 73. However, it just outputs “50)”. I tried changing some of the syntax around but I couldn’t figure it out. Any thoughts?


The example is wrong, it should be

$MyArray[7] = 23;
echo '<script>alert(' . ($MyArray[7] + 50) . ')</script>'

PHP is a bit peculiar about mixing string concatenation and math.

Maybe there is more which you do not show us, but you are also missing a semicolon at the line end.

echo '<script>alert(' . ($MyArray[7] + 50) . ')</script>'  ;  // <--- here