0

Full disclosure - I've been away from PHP for a while so bought a book to refresh and have been going through it. When I run one of the simple scripts to illustrate "variable variables" I get the infamous "unexpected T_VARIABLE" parse error. Specifially the error I get is "Parse error: syntax error, unexpected '$temp' (T_VARIABLE) in ... "

I did look at about a dozen similar questions but none of those threads helped with this particular code.

Here is the code in question:

    <?php
    
    $numnames = 15;
     for($i=1; $i <= $numnames; $i++){
      $temp = “name$i”;
    echo htmlspecialchars($$temp).’<br />’;
    }
    
    ?>

What I would expect is something like this:

name1 name2 name3 . . . name15 I think all the brackets and parens are closed properly and semicolons are all in place (I think). Any ideas why I'm getting this error?

Cy S
  • 1
  • 1
  • 2
    You didnt give a line but, these `“ ”` are not these `" "` and these `’ ’` are not these `' '`. – AbraCadaver Jun 25 '21 at 16:02
  • additionally to what @AbraCadaver said, you can call error_reporting(E_ALL - E_NOTICE); at the beginning of your script, to avoid the "Notice: Undefined variable..." warning. – jairhumberto Jun 25 '21 at 16:06
  • 2
    And really there is no good reason to use variable variables. – AbraCadaver Jun 25 '21 at 16:07
  • @AbraCadaver Hmmm, now this is interesting. I just bought a new M1 iMac and had not noticed something until you mentioned the difference in quote marks. Whan I type a quote on my keyboard, it starts as the second example you show, but then almost instantly changes to the leaning version in your first example. I have no idea why this is happening of who it might affect things. – Cy S Jun 25 '21 at 16:30
  • 1
    As for there not being a good reason to user variable variables - that may well be true, but I'm going through a tutorial. I may or may not ever actually use this in real life. Still, I'd like to figure out why I'm getting this error. – Cy S Jun 25 '21 at 16:32
  • It a setting on the Mac, not sure if it affects all apps. You can turn it off globally or maybe just for the app you are using to code. I don't use Mac but https://support.apple.com/guide/mac-help/replace-text-punctuation-documents-mac-mh35735/mac – AbraCadaver Jun 25 '21 at 16:46
  • Ah ha! Smart quotes. Well, I turned that off and now the quotes don't lean, but code still getting that error. Weird. – Cy S Jun 25 '21 at 17:07
  • @CyS: your code looks good https://3v4l.org/p9NFE - maybe you're left confused about the warning? it's just a warning because the variable is not defined. See the output at the link, which is also a great site if you want to execute some PHP example code. – hakre Jun 26 '21 at 14:29

0 Answers0