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']) :
Related
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 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.
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'];
This question already has answers here:
PHP Difference between array() and []
(5 answers)
Closed 8 years ago.
I have an Index php page that contains a code that prints the values of an array $a=['f','a','b'];.
The code works fine on WAMP server on my computer however when I upload the page online on a server like 000webhost an error comes up that says there is unexpected '[' on line of the array. Does anyone know why this happens?
Your PHP version doesn't support the [] notation, this kind of notation is available from PHP 5.4, use $a = array('f','a','b'); instead.
This question already has answers here:
<? ?> tags not working in php 5.3.1
(5 answers)
Closed 9 years ago.
Note: I'm using PHP 5.3.3 on a CentOS 6 server.
I'm testing out a new web host and I've discovered that it is simply ignoring the <?= $var ?> blocks of PHP code. For instance, if I have this in a PHP file:
<div id='<?=$page_id?>'>
Then it does not display the value of $page_id, it writes it just as you see there. On the other hand, if I write:
<div id='<?php echo $page_id; ?>'>
Then it displays the variable as it should. On its own, this isn't a big deal. Unfortunately I have a TONNE of inherited code that uses the <?= $var ?> syntax all over the place, so I'd like to avoid having to change it all.
Does anyone know what setting I have to change in order for PHP to recognize this syntax? Our old server was running PHP 5.3.14 and it worked fine.
You need to enable short tags.