9

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?

Sam
  • 7,252
  • 16
  • 46
  • 65
dendini
  • 3,842
  • 9
  • 37
  • 74

3 Answers3

15

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
}
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 1
    found the link: https://php.net/manual/en/migration54.new-features.php inside new features they say: Function array dereferencing has been added, e.g. foo()[0]. – dendini May 09 '14 at 13:38
  • I ran into this error yesterday, on my local env i have php5.5 and on testing 5.3 :/ thanks for answer. – Petro Popelyshko Oct 06 '14 at 06:17
2

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
}
Manibharathi
  • 945
  • 6
  • 18
1

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.

Ozmah
  • 373
  • 1
  • 8