having strange trouble with this:
date_parse('2014-08-26')['hour']
It works on a php command line.
But this throws an error:
if(date_parse('2014-08-26')['hour']){ echo "works";}
syntax error, unexpected '['
Why is that? The result should be any number between 0 and 23 or nothing, nothing would it be in this case.
EDIT: yes, I am on PHP 5.3.14
Please note that versions of PHP older than 5.4 can not access array attributes from a functions return value directly. You have to store the functions return value in a new variable and access the array from that variable.
So this code
if(date_parse('2014-08-26')['hour']){ echo "works";}
only works on PHP 5.4 and higher. If you run a PHP version lower then 5.4 your code should look like this:
$parseResult = date_parse('2014-08-26');
if($parseResult['hour']) { echo "works"; }
You must be using below 5.4 version of PHP. If so than please use the latest version of php as the version below 5.4 does not allow brackets [].
Before version 5.4, PHP didn't allow you to dereference elements of arrays returned from function calls without using a temporary variable:
http://php.net/manual/en/language.types.array.php#example-102
$cond = date_parse('2014-08-26')['hour'];
if($cond)
{
echo "works";
}
Hope this works
Related
Hi i have a new server runing php 5.4 on rackspace and there is errors con my code
i cannot do this becouse its giveme a error:
if(empty($basics->conversions*100) || empty($basics->activations))
this is the problem: ($basics->conversions*100);
i have to do $vr = $basic->conversions*100;
if(empty($vr)) but i have this all over my code and i cannot fix it
Parse error: syntax error, unexpected '*', expecting ')' in ***
the other error is when using a function returning an array and accesing that array insted of assign it to a variable example:
getReports($date, $todate)['utm'];
this gives me error.
but if i do:
$arrReportes = getReports($date,$todate);
$arrReports['utm'];
Works perfectly why ? can you helpme i cannot find any on google
From http://php.net/manual/en/function.empty.php
Prior to PHP 5.5, empty() only supports variables; anything else will
result in a parse error. In other words, the following will not work:
empty(trim($name)). Instead, use trim($name) == false.
This explains why your first error is occurring - empty() just doesn't let you pass an expression to it like you are doing in the version of php you said you are running (5.4).
The second error should really have been put in a separate question. In theory what you are trying to do should be possible since php 5.4 -
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.
(from http://php.net/manual/en/language.types.array.php)
- but you claim to be running 5.4 so I'd imagine there's an issue somewhere else. I'd double check the version of php you're running, and ensure your code isn't at fault there too. You didn't specify what error you were getting, so it could well be that the returned array is null, or anything.
Is it possible that this PHP code line
if ($this->greatestId()["num_rows"] > 0)
works in PHP 5.5 and returns an error in 5.3??
PHP Parse error: syntax error, unexpected '[' in /var/www/app/AppDAO.php on line 43
How can I change it to work under PHP 5.3?
Array dereferencing became available in PHP 5.4 That's why this doesn't work in PHP 5.3. So you have an extra step where you need to get the array value from your function call and then you can use it:
$variable = $this->greatestId();
if ($variable["num_rows"] > 0){
// do stuff
}
You cant use like this if ($this->greatestId()["num_rows"] > 0) in PHP 5.3 ver use below code.
$var = $this->greatestId();
if ($var["num_rows"] > 0){
// your code
}
As mentioned in the PHP 5.4 notes:
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.
It's not possible to do that in PHP 5.3, you need to use a variable.
In PHP, I'm getting "Parse error: syntax error, unexpected '['" on the following line of code:
return explode(" ", $message)[0];
I'd give more details, but that's really about it. The line looks unimpeachable to me, and in fact was running fine until I copied it into another file. The biggest difference is that now that line is part of a class, and that it's being called by a big software library that I don't know much about. What could be causing this issue?
A bit more context:
public function getIRCCommand($message)
{
if ($message[0] != ":")
{
return explode(" ", $message)[0];
}
else
{
return preg_split("/\s+/", $message)[1];
}
}
You're likely running on PHP 5.3 or lower. You need to upgrade to at least PHP 5.4 to get this working (or use a temporary variable to access elements of a function returning an array).
What you're doing there is called array dereferencing. The manual says:
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.
As of PHP 5.5 it is possible to array dereference an array literal.
And, to check that, let's try to run
<?php
echo explode(" ", "1 2 3")[1];
for all kinds of diferent PHP versions. The results are, that PHP versions 5.4 and hihgher output "2", which would be your desired result.
I have a Form object $form. One of its variables is a Field object which represents all fields and is an array (e.g $this->field['fieldname']). The getter is $form->fields().
To access a specific field method (to make it required or not for example) I use $form->fields()['fieldname'] which works on localhost with wamp but on the server throws this error:
Parse error: syntax error, unexpected '[' in (...)
I have PHP 5.3 on the server and because I reinstalled wamp and forgot to change it back to 5.3, wamp runs PHP 5.4. So I guess this is the reason for the error.
How can I access an object method, which returns an array, by the array key with PHP 5.3?
Array dereferencing is possible as of PHP 5.4, not 5.3
PHP.net:
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.
Array dereferencing as described in the question is a feature that was only added in PHP 5.4. PHP 5.3 cannot do this.
echo $form->fields()['fieldname']
So this code will work in PHP 5.4 and higher.
In order to make this work in PHP 5.3, you need to do one of the following:
Use a temporary variable:
$temp = $form->fields()
echo $temp['fieldname'];
Output the fields array as an object property rather than from a method:
ie this....
echo $form->fields['fieldname']
...is perfectly valid.
Or, of course, you could upgrade your server to PHP 5.4. Bear in mind that 5.3 will be declared end-of-life relatively soon, now that 5.5 has been released, so you'll be wanting to upgrade sooner or later anyway; maybe this is your cue to do? (and don't worry about it; the upgrade path from 5.3 to 5.4 is pretty easy; there's nothing really that will break, except things that were deprecated anyway)
As mentioned array dereferencing is only possible in 5.4 or greater.
But you can save the object and later on access the fields:
$fields=$form->fields();
$value=$fields['fieldname']
...
AFAIK there is no other option.
Are their any good resources when you are trying to get a PHP script written in a newer version to work on an older version; Specifically 5.4 to 5.3?
I have even checked out articles about the changes and I cannot seem to figure out what I am doing wrong.
Here is the error I am getting, at this moment:
Parse error: syntax error, unexpected '[' in Schedule.php on line 113
And the code it is referring to:
private static $GAMES_QUERY = array('season' => null, 'gameType' => null);
.....
public function getSeason(){
$test = array_keys(self::$GAMES_QUERY)[0]; //<<<<<<<<<< line:113
return($this->query[$test]);
}
Everything I have seen seems to say that 5.3 had self::, array_keys, and the ability to access arrays like that.
try...
$test = array_keys(self::$GAMES_QUERY);
$test = $test[0];
If I'm not mistaken, you can't use the key reference [0] in the same declaration in 5.3 like you can in 5.4 and javascript, etc.
That syntax was actually added in 5.4: http://docs.php.net/manual/en/migration54.new-features.php
So, you'll need a temporary variable to hold the result of the function, then access the index you want.
In versions lower than PHP 5.4 for the case you have you can make use of the list keywordDocs:
list($test) = array_keys(self::$GAMES_QUERY);
That works in PHP 5.4, too. But it does deal better with NULL cases than the new in PHP 5.4 array dereferencingDocs.