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
Related
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)));
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
I am trying to get a WordPress site working on a server running PHP 5.3. I am not able to update the server so am trying to make things compatible.
I am getting the following error...
Parse error: syntax error, unexpected '['
The line that is causing the error is...
echo wp_get_attachment_image($mysection['imageid'], 'medium', "", ["class" => "side_img"] );
Any ideas how to modify this code to be compatible?
The short array syntax was first introduced in PHP 5.4. PHP 5.3 does not understand what ["class" => "side_image"] is, hence the syntax error.
The solution is simple, change:
["class" => "side_image"]
into:
array("class" => "side_image")
PHP 5.3 does not support the "short array syntax" like [1, 2, 3, 4]. These must be converted to array(1,2,3,4).
See here: http://php.net/manual/en/migration54.new-features.php
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:
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'];
This question already has answers here:
PHP syntax for dereferencing function result
(22 answers)
Closed 9 years ago.
My script is working really fine on my xampp. Now I tried to upload it on the server, but it spat directly a
Parse error: syntax error, unexpected '['
in my face. :(
The line which its mocking about is this one:
$item = $xml->xpath($path)[0];
And I have no idea what is wrong. I tried to look on the php 5.3 changelog but did not found anything about it. (Because I have 5.3 on the server, and on xampp its an olderversion)
The whole code block looks like this:
$path = '//item[#id="'.$id.'"]';
if ($xml->xpath($path)) {
$item = $xml->xpath($path)[0];
} else {
die('<p class="error">Script Error: Code 101 - Please contact administrator</p>');
}
I am thankful for any help, I cannot seach [ with google and have no idea where it could come from, since on xampp its working fine
Try this
$item = $xml->xpath($path);
$item = $item[0];