0

I have PHP 5.2 and am trying to use this anonymous function

$values = array_map(function ($value) use ($link){
    if($value == null) return null;
    return mysqli_real_escape_string($link, (string)$value);
}, array_values($input));

Server's response

PHP Error Message Parse error: syntax error, unexpected T_FUNCTION in MY FILE on line 16

So server wont read this anonymous function, so i gotta define it right?

$func = function ($value) use ($link){
    if($value == null) return null;
    return mysqli_real_escape_string($link, (string)$value);
};
$values = array_map($func, array_values($input));

Still now working. Any help?

Machavity
  • 30,841
  • 27
  • 92
  • 100
Mercurial
  • 3,615
  • 5
  • 27
  • 52
  • 2
    ?? Server PHP Version ?? – RiggsFolly Nov 10 '16 at 22:35
  • Use prepared statements and then you wont need to do any `mysqli_real_escape_string` stuff – RiggsFolly Nov 10 '16 at 22:37
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Nov 10 '16 at 22:37
  • `PHP version 5.2.*`. And thanks for informing me about SQL injections. – Mercurial Nov 10 '16 at 22:39
  • I wouldn't use that hosting service. It runs PHP5.2 which is yeeears out of date and a quick google shows that they require you to pay for a more up to date PHP version - terrible! – Scopey Nov 10 '16 at 22:42
  • Take a look at this example http://php.net/manual/en/function.array-map.php#117813 – kyle Nov 10 '16 at 22:43
  • Just as an FYI, PHP 5.2 is missing nearly a decade of security patches. I wouldn't host anything on that server – Machavity Nov 10 '16 at 22:59
  • @all I understand the security risks you all are talking about and i also understand nothing good comes free. But as for now my requirements dont need any security concerned servers, just a server able to parse my small snippets. I will be using AMAZON servers when i'll run into deploy from dev. – Mercurial Nov 10 '16 at 23:20

1 Answers1

0

Anonymous functions became available as of PHP 5.3, so 5.2 doesn't have them

This should work in all versions of PHP

function array_stuff($value){
    global $link; // I hate this sooo much but necessary evil here
    if($value == null) return null;
    return mysqli_real_escape_string($link, (string)$value);
};
$values = array_map('array_stuff', array_values($input));
Machavity
  • 30,841
  • 27
  • 92
  • 100