Parse error: syntax error, unexpected T_FUNCTION line 10?
Asked Answered
W

6

17

What is wrong with my code? I ran the code on my test server and the code worked but when I upload it to my production server I get

Parse error: syntax error, unexpected T_FUNCTION in /hermes/bosweb/web013/b130/ipg.acrsflcom/darayngedbeats/gentest.php on line 10

here is my code

$old = "http://darayngedbeats1.s3.amazonaws.com    /mp3/CrazyMonsta2.mp3?AWSAccessKeyId=AKIAJXA36ESCLQHCB54Q&Expires=1297279906& Signature=HD36ZQE8yeTIW6JPWKMcciPTiTs%3D"; //enter the key that needs to be converted
$search =  array(":","?","=","&","%");
$replace = array("%3A","%3F","%3D","%26","%25");

function search_replace($s,$r,$sql)
{ $e = '/('.implode('|',array_map('preg_quote', $s)).')/';
  $r = array_combine($s,$r);
  return preg_replace_callback($e, function($v) use ($s,$r) { return $r[$v[1]];  },$sql);
}

echo "<br><br>";
$new = search_replace($search,$replace,$old);
echo $new;

?>
Wimmer answered 9/2, 2011 at 19:34 Comment(4)
i presume it has to do with the callback functionMckean
line 7 return preg_replace_callback($e, function($v) use ($s,$r) { return $r[$v[1]]; },$sql);Wimmer
Q: "Which line is line 10?" - A: "line 7 [code]" I lol'd.Dermatome
line 7 is 10 return preg_replace_callbackWimmer
D
32

The error is likely caused by

return preg_replace_callback($e, function($v) use ($s,$r) { return $r[$v[1]];  },$sql);

Chances are you're using PHP 5.2 or earlier, which doesn't support closures. You can find out which version of PHP you're using phpinfo().

You'll likely either need to upgrade to PHP 5.3+, or use create_function, or write a static function and pass it as a callback.

Here's an example of the last option, using a simple class to store the state of $r:

class My_callback {
  public function __construct($s, $r) {
    $this->s = $s; $this->r = $r;
  } 

  function callback($v) { return $this->r[$v[1]]; }
}

function search_replace($s,$r,$sql) {
  $e = '/('.implode('|',array_map('preg_quote', $s)).')/';
  $r = array_combine($s,$r);
  $c = new My_callback($s, $r);
  return preg_replace_callback($e, array($c, 'callback'), $sql);
}
Divergence answered 9/2, 2011 at 19:37 Comment(7)
ok i tryed this $newfunc = preg_replace_callback($e, create_function($v) use ($s,$r) { return $r[$v[1]]; },$sql); now i get Parse error: syntax error, unexpected T_USE in /hermes/bosweb/web013/b130/ipg.acrsflcom/darayngedbeats/gentest.php on line 10Wimmer
@user514584: use does not exist before PHP 5.3 either. You should have read the documentation @meagar linked to, to see how create_function works. Here again: php.net/manual/en/function.create-function.php. Unfortunately, you cannot create closures with create_function so you have to think about a different way how to access $r and $e in the callback.Kumar
@user Try just writing a regular function and passing it in as a string.Divergence
doesn't passing a function in as a string use eval()? eval is evil as i recall. It opens too many security holes. Just extract and name it.Solferino
@Scott I'm talking about a callback, the same thing you'd pass to call_user_func. It has nothing to do with eval.Divergence
@Felix I'm new to php i read the documentation and i did not understand it. i didn't make the scrip that I'm trying to use just made modifications to it so can someone give me an exampleWimmer
ah ok. I misunderstood. yeah that makes perfect sense then.Solferino
M
12

For anyone getting this error on PHP 5.3+ and especially with a wordpress theme, I would recommend having a look at the formatting of the actual files on the server.

When I encountered this error and viewed the PHP files throwing the error on the server, they had no line breaks and were effectively minified to one line.

For some reason, Filezilla stripped out the line breaks when I uploaded the files and this was what was causing this same error to occur.

By changing the transfer type in Filezilla to Binary (Transfer > Transfer Type > Binary) and re-uploading the wordpress theme, this fixed my issue!

I hope this helps someone!

Marrufo answered 18/8, 2015 at 4:22 Comment(3)
Thank you so much for this!! Would never have thought to try that but it did indeed fix my 500 errors :)Arsonist
Thank you for sharing this, this really helped meNeuritis
This was a godsend for me. I was looking for HOURS trying to find the answer and this finally fixed it. Thank you so much for posting this!!Control
S
4

try extracting your callback function into a separate named function and referring to it by name.

Solferino answered 9/2, 2011 at 19:38 Comment(0)
M
3

I think you are looking for create_function: http://php.net/manual/en/function.create-function.php

create_function is supported both in php4 and php5

Mckean answered 9/2, 2011 at 19:38 Comment(0)
B
2

By now this question is mostly obsolete because 5.3 has been around for a long time, but besides the points raised by the other answers, I would like to point out that what you're trying to do can already be done using strtr():

$new = strtr($old, array(
  ':' => '%3A',
  '?' => '%3F',
  '=' => '%3D',
  '&' => '%26',
  '%' => '%25',
));
Barleycorn answered 9/2, 2011 at 19:34 Comment(0)
S
1
PHP Parse error:  syntax error, unexpected 'function' (T_FUNCTION)

In my case, I fixed this error, by adding a semicolon that I've forgotten at the end of a variable declared previously..

Superjacent answered 6/6, 2021 at 20:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.