-2

It must be a simple question but would like to know, how I can write below for php 5.3 compatible?

 $parts = explode(' ', $data['start-line'], 3);
$version = isset($parts[2]) ? explode('/', $parts[2])[1] : '1.1';

Like in second line I am getting unexpected [ error because of php 5.3 is not allowed to directly write lines like explode('/',$parts[2])[1] - as [1] is not allowed to write directly in php 5.3 so either I have to write like $exp = explode('/',$parts[2]); $exp[2] am I right?

Is it following right?

$parts = explode(' ', $data['start-line'], 3);
$exp = explode('/', $parts[2]);
$version = isset($parts[2]) ? $exp[1] : '1.1';

Second Issue Second line issue on the following code as well:

  if (!is_array($result[$key])) {
            $result[$key] = [$result[$key]];

How I can write $result[$key] = [$result[$key]] line compatible with php 5.3? As above line returning unexpected [ error as well.

Second Issue Whole code:

function parse_query($str, $urlEncoding = true)

{ $result = array();

if ($str === '') {
    return $result;
}

if ($urlEncoding === true) {
    $decoder = function ($value) {
        return rawurldecode(str_replace('+', ' ', $value));
    };
} elseif ($urlEncoding === PHP_QUERY_RFC3986) {
    $decoder = 'rawurldecode';
} elseif ($urlEncoding === PHP_QUERY_RFC1738) {
    $decoder = 'urldecode';
} else {
    $decoder = function ($str) { return $str; };
}

foreach (explode('&', $str) as $kvp) {
    $parts = explode('=', $kvp, 2);
    $key = $decoder($parts[0]);
    $value = isset($parts[1]) ? $decoder($parts[1]) : null;
    if (!isset($result[$key])) {
        $result[$key] = $value;
    } else {
        if (!is_array($result[$key])) {
            $result[$key] = [$result[$key]];
        }
        $result[$key][] = $value;
    }
}

return $result;

Thanks please explain to me how I can interpret this line of code.

Jimil
  • 650
  • 2
  • 14
  • 37
  • 2
    The better question is why you're currently running / attempting to run PHP 5.3. It's been [**obsolete since August 2014**](https://www.php.net/eol.php). In fact, PHP 5.5 stopped receiving security updates [**back in 2016**](https://www.php.net/supported-versions.php), and even PHP 7.2 is stopping active support in a few months. Please consider upgrading. – Obsidian Age Oct 03 '19 at 22:15
  • Hi @ObsidianAge we are doing upgrade currently but for the time being we have to support the older version as it's a working system. so that's kind of odd situation but I have to find a way. thanks – Jimil Oct 03 '19 at 22:20

1 Answers1

1

This should work in an identical way to the original code. The original code however doesn't do a good job of checking for errors. For example, you may want to check if $temp[1] is set, and then return it, otherwise, return "1.1". But, that's not what the original code did.

$parts = explode(' ', $data['start-line'], 3);
$version = call_user_func( function() use( $parts ){
    if ( isset( $parts[2] ) ) {
        $temp = explode( '/', $parts[2] );
        return $temp[1];
    }
    return "1.1";
});

I can accomplish the same thing below in a way that's probably easier to understand. The only difference with below is that the $temp variable lives in the same variable scope as possibly other variables. If you had another $temp variable defined, then the code below could have unintended side effects. The code above takes some additional steps to ensure side effects are not possible, given that I don't know what the surrounding code is.

$parts = explode(' ', $data['start-line'], 3);
if ( isset( $parts[2] ) ) {
    $temp = explode( '/', $parts[2] );
    $version = $temp[1];
} else {
    $version = "1.1";
}
Joel M
  • 353
  • 2
  • 10
  • thanks for your feedback. People are down voting for my question rather than simply give an answer. I am stuck in the situation where I am trying Guzzle code modifying to make it work for php 5.3 version. – Jimil Oct 03 '19 at 22:39
  • Can you able to provide me some explanation for my second issue which I've asked in the above question? – Jimil Oct 03 '19 at 22:40
  • Even I don't know this is code provided by Guzzle inside the folder vendor/guzzlehttp/psr7/src/functions.php file – Jimil Oct 03 '19 at 22:44
  • Do you know why people write that way and it's really a mean anything? I am writing that whole function for you here. – Jimil Oct 03 '19 at 22:44
  • To be fair, I'm a bit concerned that you are trying to fix PHP incompatibility issues when it appears that maybe you don't have too much experience, but I prefer to answer peoples questions rather than tell them why they shouldn't be doing it in the first place. You should also not assume that my code is correct, it doesn't matter how experienced you are you, you can still make typos on the simplest of things. – Joel M Oct 03 '19 at 22:45
  • To be completely honest with you, I don't even know what this means: $result[$key] = [$result[$key]]. (I've never seen square brackets used on the right hand side like that) – Joel M Oct 03 '19 at 22:49
  • 1
    Also, your question probably is off-topic and I wouldn't be surprised if it got closed. But I don't mind helping with something simple. As for your second request though, I'm not sure if I will get around to answering it. – Joel M Oct 03 '19 at 22:51
  • Hi Yes, same here I never seen this type of code before as well but my goal is to simply pass this error so that guzzle code works and run.:( Not sure what I can do best here. However, everything start from one of my question here: https://stackoverflow.com/questions/58128189/how-to-resolve-please-ensure-that-the-client-is-sending-referer-or-use-the-api-c?noredirect=1#comment102735603_58128189 – Jimil Oct 03 '19 at 22:51
  • and than second question here: https://stackoverflow.com/questions/58177292/how-to-resolve-syntax-error-unexpected-in-guzzle?noredirect=1#comment102744795_58177292 – Jimil Oct 03 '19 at 22:52
  • Since this two questions I am in the loop to get something fixed. Let me know if you can help me with any of above questions. – Jimil Oct 03 '19 at 22:53
  • Thanks for your help tho. – Jimil Oct 03 '19 at 23:00