1

I have this php function :

    function soheiladder($old_h,$old_m,$new_m)
{
    $old_total_m = ($old_h * 60 ) + $old_m ;
    $new_total_m = $old_total_m + $new_m ;

    $output_h = intval($new_total_m  / 60 );
    $output_m = ($new_total_m % 60);
    $output =array();
    $output[0] = $output_h; // saat
    $output[1] = $output_m; // minute
    return $output;

}

when I want to use it's output in this way :

echo  soheiladder($pele_h ,$pele_m,$ajib) [0] ;
echo  soheiladder($pele_h ,$pele_m,$ajib) [1] ;

It works well in wamp server but when I test it online php says :

Parse error: syntax error, unexpected '[' in ...

for each output ! what should I do ?

SoheilYou
  • 111
  • 1
  • 1
  • 16
  • 1
    Array dereferencing under 5.4 doesn't work! – Rizier123 Jan 29 '15 at 17:41
  • @Rizier123 - Not to mention that in this specific case it's inefficient. You end up having to fun the `soheiladder` function twice because you never stored the results. – Mr. Llama Jan 29 '15 at 17:42
  • Why are you developing on a different version of PHP to the version you're going to be using on your production servers? It's 2015. With the ease of setting up virtual boxes, there's no need to have different stacks for development and production – Mark Baker Jan 29 '15 at 17:48
  • @MarkBaker: I don't totally agree with you, developing on different platform helps you to create a software that is later more portable (such in this case where the OP found this issue) – dynamic Jan 29 '15 at 17:50
  • If you're developing for different platforms, such as when you're developing an open source library or framework, when you don't know what environments people will run it against, then have a range of virtual machines reflecting a range of different versions of PHP, different configurations, etc, and it simplifies testing it under those different configurations.... tools like vagrant/virtualbox make this easy, and tools like docker allow you to build and save images for different VMs easily – Mark Baker Jan 29 '15 at 17:55
  • Im Just writing the code and I'll give it to someone else ! so I can not upgrade .. I should prevent to create any errors for any php version ! – SoheilYou Jan 29 '15 at 18:08

2 Answers2

4

Because that syntax (namely Function Array Dereferencing) is not supported in every PHP version.
It has been implented since PHP 5.4

You can read the RFC here: https://wiki.php.net/rfc/functionarraydereferencing

The funny thing is that in 2009 it was first declied and finally reapproved and added some years later.

Keep in mind that given your snippet, you should cache the result:

$cached = soheiladder($pele_h ,$pele_m,$ajib);
echo $cached[0], $cached[1];
dynamic
  • 46,985
  • 55
  • 154
  • 231
1

Your version of PHP is obviously less than 5.4 as it supports function array dereferncing so first upgrade PHP

Alternatively store output in a variable and then get the appropriate key

Shaun Hare
  • 3,771
  • 2
  • 24
  • 36