Parse error: syntax error, unexpected '[' with php 5.3 [duplicate] - php

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];

Related

PHP function giving error in my system [duplicate]

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.

PHP Parse error: syntax error, unexpected '[' [duplicate]

This question already has an answer here:
Difference in accessing arrays in PHP 5.3 and 5.4 or some configuration mismatch?
(1 answer)
Closed 7 years ago.
I got few lines producing error 500. The error log says:
PHP Parse error: syntax error, unexpected '[' in...
and the lines are:
$catids[] = $params->get('catid');
and after commenting it out, this one comes up:
$return_category[] = self::_getCategoryInfor($tg, $params)[0];
How can i modify them in order to match with new PHP versions and solve the error 500?
From your code, I do not see code that may triggered this parse error. Try change your code so it becomes:
$categories = self::_getCategoryInfor($tg, $params);
$return_category[] = $categories[0];
This is the expression causing the error:
self::_getCategoryInfor($tg, $params)[0];
Function array dereferencing was added in PHP 5.4. Your code will generate a parse error on 5.3 or lower:
https://secure.php.net/manual/en/migration54.new-features.php

PHP Code works well in XAMPP but results an error at my webhost [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I have a local XAMPP installation for testing and an webserver for live testing. At my local environment, everything works well. But when I load it up to my server, it gives me the error:
Parse error: syntax error, unexpected '[' in /data/web/e36087/html/pdf/index.php on line 11
But everything seems correct. What can I do?
PHP-Code:
<?php
setlocale(LC_TIME, 'de_DE.UTF-8');
$xmldb = "db.xml";
$xml = simplexml_load_file($xmldb);
if(isset($_POST["remove"])) {
unlink($xml->xpath('/data//count['.$_POST["remove"].']/fulldir')[0][0]);
unlink($xml->xpath('/data//count['.$_POST["remove"].']/pdfdir')[0][0].$xml- >xpath('/data//count['.$_POST["remove"].']/filename')[0][0]);
$query = $xml->xpath('/data//count['.$_POST["remove"].']')[0][0];
unset($query[0][0]);
//Datei schreiben
$fopen = fopen($xmldb, "w");
fwrite($fopen, $xml->asXML());
fclose($fopen);
}
?>
Link to my Webhost: http://livetest.philipgraf.at/
Function array dereferencing was first added in PHP 5.4. It's likely that your host is running 5.3.
In short this means that
unlink( $xml->xpath('/data//count['.$_POST["remove"].']/fulldir')[0][0] );
must be written as this:
$path = $xml->xpath('/data//count['.$_POST["remove"].']/fulldir');
unlink( $path[0][0] );
So essentially, save your method result to a variable before using indexes.

PHP error unexpected '[' with php 5.3 [duplicate]

This question already has an answer here:
PHP unexpected '['
(1 answer)
Closed 8 years ago.
I can not use a newer PHP version then 5.3 and i get this error message:
**Parse error: syntax error, unexpected '[' in PATH/product.php on line 17**
I founded on this page that is php version problem, how can i solve this in php 5.3?
The code on this line is exactly:
$_productids = [];
Old version doesn't support this syntax. Update this to:
$_productids = array();
Use old array syntax:
$_productids = array();

SVN Commit failed with syntax error, unexpected '[' [duplicate]

This question already has answers here:
Unexpected bracket '[' - PHP [duplicate]
(3 answers)
Closed 9 years ago.
I 'm try to commit this code to my server from my Ubuntu server
$me = $this->fetchAll(array('id'=>$Id,'ce'=>$e), array('cr'=>array('$slice' =>[$offset, $limit])));
but it return
PHP Parse error: syntax error, unexpected '[' in - on line 114
MSG: Failed to checkin branches/myFile.php, PHP said
what is the wrong with '$slice' =>[$offset, $limit] in order to commit it to my server
The []-short-array notation was introduced with PHP 5.4. Some *ix distros have still not introduced this new version to their default repos. Make sure your server uses php5.4 to parse that specific file. For the time beeing I'd recommed to use the 'old' array notation.
http://www.php.net/manual/en/migration54.new-features.php
You can't do it like that. Try this:
'$slice' => $offset . ', ' . $limit
I think that's what you're looking for.

Categories