Unexpected T_FUNCTION for function on PHP 5.2 [duplicate] - php

This question already has an answer here:
Making anonymous functions from PHP 5.3 work with PHP 5.2
(1 answer)
Closed 8 years ago.
I'm using the following function:
function uniqueACF(array $array) {
$flatten = array();
array_walk_recursive($array, function($value) use(&$flatten) {
$flatten[] = $value;
});
$flatten = array_unique($flatten);
return $flatten;
}
This works fine in PHP 5.3, but for PHP 5.2 it will give a 'unexpected T_Funcion for the array_walk_recursive line. It probably has to do with the function($value) or use(&$flatten).
But I'm not sure how I should make this work on PHP 5.2. How should I rewrite this for PHP 5.2?

Anonymous functions are available from PHP 5.3. That's why it doesn't work in PHP 5.2.
You should really think, why you move to PHP 5.2 - even PHP 5.3 reached already end of life and PHP 5.6 will be released soon.

Related

PHP Function create_function() is deprecated , the code cant work in newest PHP? [duplicate]

This question already has answers here:
PHP 7.2 Function create_function() is deprecated
(6 answers)
Closed 1 year ago.
I have a function which im not entirely sure how to convert it to get working in newest php
$eventSponsor = array_map(create_function('$o', 'return $o["id"];'), $event->sponsors);
which method should i use in newest php version ?
yeah i was searching and i found this method called anonymous function so the code be like
$awe function ($o) {
return $o["id"];
};
$eventSponsor = array_map($awe,$event->sponsors); ```

PHP 7.3 Function create_function() deprecated from 5.6 update [duplicate]

This question already has answers here:
PHP 7.2 Function create_function() is deprecated
(6 answers)
Closed 2 years ago.
I have recently upgraded a site im working on from PHP Version 5.6 to 7.3
The problem I am having is with a deprecated function in my wordpress functions.php
add_filter('max_srcset_image_width', create_function('', 'return 1;'));
How would i rewrite the above code for 7.3
I have already tried:
add_filter('max_srcset_image_width', function('', 'return 1;'));
Thanks
You can use functions directly like closures.
add_filter('max_srcset_image_width', function() { return 1; });
Update:
Since PHP 7.4 you can use shorthand arrow functions and write it shorter.
add_filter('max_srcset_image_width', fn() => 1);

How to use array_filter with callback in php 5.2 [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I am trying to use array_filter with call back in php 5.2, but I get the following error:
Parse error: syntax error, unexpected T_FUNCTION
And I did search the solution using the error in Google search and found that Php 5.2 does not support callback. The code I am working on is:
$result = array_filter($lines, function($line) {
return stripos($line,"ID:")!==false;
});
How do I change it so that It can work in php 5.2? Any help and workaround would be very much appreciated. Thanks.
Anonymous functions was introduced in PHP 5.3, so if you are using PHP 5.2 or lower you need to define the function explicitly and pass the functions name as the second argument of array_filter(), as shown below.
$result = array_filter($lines, 'filter');
function filter($line) {
return stripos($line,"ID:") !== false;
}
Consider upgrading to a newer version of PHP if you can.
PHP.net on array_filter()
PHP.net on anonymous functions
PHP.net on the Closure class

PHP Syntax Error for simple web script [duplicate]

This question already has an answer here:
Populating associative arrays
(1 answer)
Closed 7 years ago.
Running into an issue with a simple PHP script and I can't seem to figure it out. States its on line 3 and I don't see it. Need a fresh set of eyes please.
<?php
$numArr = [];
for ($i=0;$i<5;$i++) {
array_push($numArr,mt_rand());
}
echo min($numArr);
?>
It's likely how you're initializing your array. The [] syntax is only available from PHP 5.4 and above.
From the PHP manual:
As of PHP 5.4 you can also use the short array syntax, which replaces
array() with [].
You can use array () instead.

Lower versions of php complaining about brackets. Why? [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I have been developing locally using Mamp, and everything was great until I uploaded to the server. I have narrowed my problem down to having to do with the php version. Mamp was running on a newer version than the server.
If I run Mamp on PHP 5.6.2(or 5.5.X) I have no problems with my code. But if all I do is change the PHP version in Mamp preferences to PHP 5.3.29 if complains about the following line of code:
$shipping = reset($arrShipOptions['options'])[0]['price'];
The error is:
syntax error, unexpected '['
First thing that came to mind was that reset() might be a new function. But according to http://php.net/manual/en/function.reset.php it was already available in PHP 4
Could an extra pair of eyes shed some light on this please.
Thanks
In older PHP versions you have to assign result from reset (or any other function) to variable and then access it using [].
$shipping = reset($arrShipOptions['options']);
$shipping = $shipping[0]['price'];
before php 5.4 you cant chain syntax like that...
http://docs.php.net/manual/en/language.types.array.php
It's called array dereferencing. It is not available in php 5.3
// on PHP 5.4
$secondElement = getArray()[1];
// previously
$tmp = getArray();
$secondElement = $tmp[1];
The problem is caused by using a feature available form PHP 5.4+ called
Function array dereferencing
Source http://php.net/manual/en/migration54.new-features.php (the third feature)
The solution is breaking the code into two lines:
$shipping = reset($arrShipOptions['options']);
$shipping = $shipping[0]['price'];

Categories