SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Location
    California
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    PHP Code:
    $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?

    Thanks!

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    The example is wrong, it should be

    PHP Code:
    $MyArray[7] = 23;
    echo 
    '<script>alert(' . ($MyArray[7] + 50) . ')</script>' 
    PHP is a bit peculiar about mixing string concatenation and math.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Maybe there is more which you do not show us, but you are also missing a semicolon at the line end.
    PHP Code:
    echo '<script>alert(' . ($MyArray[7] + 50) . ')</script>'  ;  // <--- here 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •