This question already has answers here:
PHP syntax for dereferencing function result
(22 answers)
Closed 7 years ago.
I have a strange syntax problem in php.
At some point in my code, I do a mysql query (which only send back one line with one value), and I get the query result the following way :
$myvar = mysql_fetch_assoc($result)["something"];
$result being the mysql result, of course.
It works just fine, both locally and on the site.
However, when my colleague took the latest version of the site for some local test of his own, he got the following error :
Parse Error: syntax error, unexpected '[' in C:\wamp\www\mySite\ref\myFile.php on line 33
Line 33 being the line where I defined $myvar.
He fixed it by doing the following :
$myvar = mysql_fetch_assoc($result);
$myvar = $manif["something"];
It seems pretty obvious to me that the problem comes from wamp (I am personally running an apache server), so my question is "why?".
I only got into web development recently (I was more of a C++ developer until now) and I've been using this syntax for a while. Is it bad practice?
And why does wamp return an error but not apache? Do each server have their own php parser?
Thank you.
Function array dereferencing (foo()[0] for example) has been introduced in PHP5.4. Check your php version.
Since PHP 5.4 it's possible to do what you want. If you colleague use older version of PHP, that's why it's not working.
If you are using PHP prior to 5.3, you should use temporary variable, e.g. something like this:
$r = mysql_fetch_assoc($result);
$myvar = $r['something'];
Related
This question already has answers here:
Colon after method declaration?
(2 answers)
Closed 4 years ago.
It's almost impossible for me to search for an answer to this as I don't know what this syntax is called.
I have a server running a relatively old version of PHP (5.5.38) and I'm getting parse errors when I run a file that has the following:
public function foo(): array
{
...specifically the : array suffix.
Can anyone tell me what this syntax is called (so I can research further) and which PHP version introduced it?
This was added in PHP 7, they're called return type declarations.
http://php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration
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 an answer here:
Difference in accessing arrays in PHP 5.3 and 5.4 or some configuration mismatch?
(1 answer)
Closed 8 years ago.
I wrote a code that worked perfectly fine, but then I reinstalled my operating system, and now when I try to run this, I keep getting this error. I am using IIS 8.0, WebMatrix, and PHP 5.3.
This is the piece of code in question:
<?php
if (!$me->get_details()['quote']) : //<--error is here
?>
<p class="quote">Write some motivational quote.</p>
<?php
else :
?>
<p class="quote"><?= $me->get_details()['quote']?></p>
<?php endif; ?>
Function get_details() returns an associative array with data from the database.
What could possibly go wrong here?
You cannot dereference in PHP 5.3 this way.
You need to do:
$result = $me->get_details();
if (!$result['quote']) :
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP syntax for dereferencing function result
If a PHP function returns an array, the following syntax will not work:
$firstValue = $object->methodThatReturnsArray()[0]; // syntax error, unexpected '['
This, however, works fine:
$temporaryArray = $object->methodThatReturnsArray();
$firstValue = $temporaryArray[0]; // temporary will never be reused
What is the best syntax to solve this problem, or is creating that variable the recommended approach?
Variable is the best approach.
Still PHP 5.4 adds the feature to be able using the first mentioned syntax.
Well, you can have list to the left of the = (list($firstValue) = $object->methodThatReturnsArray();), if you need anything too deep in the array, a temporary variable is your only option.