-6

I have this line in PHP:

  function remove_dangerous_chars($src) {
        $list = array( '$', '\', '"', '\', '_REQUEST', '_GET', '_POST', '_COOKIE', '_FILES', '_SERVER', '_ENV', 'GLOBALS', '_SESSION', 'toupper' );

When I run it, I get the error:

Parse error: syntax error unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')'

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
mauyr justin
  • 37
  • 1
  • 1
  • 3
  • 3
    Your question is unstated, so we'll have to guess what you meant. Here are some tries: _How do I perform basic debugging?_ _Where can I learn about PHP syntax_? Feel free to edit these or other questions into the body of your post. – George Cummins Sep 10 '14 at 21:34
  • The code you posted is missing the closing brace. Also, it doesn't do anything with the input. Can you provide a little more context? – htxryan Sep 10 '14 at 21:35
  • 1
    Instead of giving you a copy paste solution, I'll suggest you **learn** the syntax of the language. 2 questions on SO and you got downvotes, both of them being about the syntax. – N.B. Sep 10 '14 at 21:35
  • if you can help so sank you – mauyr justin Sep 10 '14 at 21:35
  • 1
    The backslash you've enclosed in a couple of your strings is escaping the closing quote, and wrecking the syntax. Try using `'\\'` –  Sep 10 '14 at 21:35
  • SO syntaxc highlighter should provide a clue, though your own code editor/IDE should have done the same – Steve Sep 10 '14 at 21:35
  • 1
    http://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them – georg Sep 10 '14 at 21:36

2 Answers2

3

You need to escape a character, the backslash, in your array -

$list = array( '$', '\\', '"', '_REQUEST', '_GET', '_POST', '_COOKIE', '_FILES', '_SERVER', '_ENV', 'GLOBALS', '_SESSION', 'toupper' );

And you only need the backslash once in the array.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
2

It is because you accidentally escaped the single quote, so the parser interpreted the quote as literal character, causing the error in your code.

..., '\', ...
      ↑

The backslash is an escape character. It causes the next character to be interpreted as literal character. So the quote right behind the backslash does not end the string, it is interpreted as a literal single quote. That causes the syntax error.

In order to fix the error, you must escape the backslash itself, so it is not an escape character anymore, but a literal backslash:

..., '\\', ...

The first backslash causes the second backslash to be interpreted literally.

$list = array('$', '\\', '"', '_REQUEST', '_GET', '_POST', '_COOKIE', '_FILES', '_SERVER', '_ENV', 'GLOBALS', '_SESSION', 'toupper');
MC Emperor
  • 22,334
  • 15
  • 80
  • 130