This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
i have problems with part of my code. In my local machine this code works, i have php7
$totals = array_values(
array_count_values(
array_map(function($x)
{
return explode('-', $x)[0];
}, $arraySKU2)
)
);
This is the error that i get on server that have php version 5.3.3.
Parse error: syntax error, unexpected '[' in
I tried to find solution on internet, but i didnt make it.
Can someone help me please, i dont know how to write changes?
This is not working:
return explode('-', $x)[0];
Try assiging the result of explode to a variable and access the first element from that variable.
According to the release notes for PHP 5.4, accessing data this way has not been possible in versions older than 5.4.0
Finally, your code could look like this:
$totals = array_values(array_count_values(array_map(function ($x) {
$explodedValues = explode('-', $x);
return $explodedValues[0];
}, $arraySKU2)));
Related
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
I don't know why the code below giving an error on my laptop while not at my friend's.
<?php
function myfunction() : int {
return 10;
}
echo myfunction();
?>
Error
Parse error: syntax error, unexpected ':', expecting '{' in (my location) on line 2.
If I remove the ": int" on line 2 everything is fine, but can someone explain why this code can't run on mine?
Please read the documentation. It is a PHP7+ only feature.
What might be a good idea as a work around, until you migrate to PHP7, is to do the following:
function myfunction() {
return (int)10;
}
var_dump(myfunction());
That will convert the return to an integer.
It's worth noting, this won't throw any warnings if the return value cannot be resolved.
I.E. If you passed parameters and those parameters were letters in a string, for instance, you'd get Warning: A non-numeric value encountered. However, for now, I think the above solution will suffice.
I strongly recommend upgrading to the latest version of PHP, though.
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I was running the following line on PHP5.4 without any problem:
$lstContent = $data->xpath("/response/lst[#name='highlighting']")[0]->lst[$i]->arr->str;
But now on PHP5.3 (production system) I get the following error:
Parse error: syntax error, unexpected '[' in /var/www/html/upload/inc_suche_code.php on line 153
Any ideas for a quick fix?
Updating PHP won't work for me.
In older versions of PHP you can not access array values directly on variables that are the result of a function. You have to split up the expression using a temporary variable.
$result = $data->xpath("/response/lst[#name='highlighting']");
$lstContent = $result[0]->lst[$i]->arr->str;
As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.
Source: http://php.net/manual/en/language.types.array.php
Edit: Obligatory "you should also consider upgrading your PHP version". This annoying limitation was fixed ages ago, not to mention that the 5.3 had its end of life in 2014, meaning it has not received security upgrades since.
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
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I'm using this to determine if a variable is set. I am not a beginner and normally, this works (it's not complicated either)...
if(isset($ok)) {
[stuff happening]
}
This is the return:
Parse error: syntax error, unexpected '}' in firstlaunch.php on line 6
The thing is, that's that. My file is empty but for those few lines. I'm used to fixing those types of problems but I can't see why the error is coming since that's the only PHP on my page.
Ideas?
Thanks a lot!
++++++
Edit: the full script is:
<?php
$ok = isset($_GET['flag']);
if(isset($ok)) {
}
?>
Ok guys, thanks a lot for the HEX editor suggestion, it worked: I had this thing between my { }:
 
Wonder where that came from... But now my file executes!
Thanks!
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
Recently I moved my website from localhost to a url domain, and now it keeps showing the errors above. The lines with problems are shown bellow. I think it's because the sintax that I used is from a more recent version of PHP, but I can't find any specific documentation for previous versions.
<?php echo explode(' ', $_SESSION['usuario_nome'])[0]; ?>
...
$fields_holder += [$name => $table_info[$i][$name]];
What is the equivalent of those lines of code to PHP < 5.4?
Can anybody think of any other reason the code suddenly stopped working?
Sorry about my bad english...
PHP5.3 does not support the short array syntax, it is new in PHP5.4
The syntax that should work in PHP < 5.4 and in PHP5.4 and > is
<?php
$t = explode(' ', $_SESSION['usuario_nome']);
echo $t[0];
$fields_holder[] = array($name => $table_info[$i][$name]);
But I would guess that there is going to be more code that you are going to have to refactor if you have implemented this code on a server that is running PHP5.3
In previous version you have to use old double-line syntax:
$array = explode(' ', $_SESSION['usuario_nome']);
echo $array[0];
$array = array( $name => $table_info[$i][$name] );
The += syntax with array is unsupported even in PHP > 5.3