6

I have a function returning an array, called curPageURL. On my local apache, I accessed the return-value of the Page like this: $pageUrl = explode('?',curPageURL())[0]; it worked pretty fine. But on live it didn't work. It took me a lot of time to figure out, that the error was accessing the array.

This solved the issue:

$pageUrl = explode('?',curPageURL());
$pageURL = pageURL[0];


function curPageURL() {
        $pageURL = 'http';
        if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
        $pageURL .= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
        } else {
            $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
        }
        return $pageURL;
    }
  • Can anybody explain why?

  • Is it forbidden to access array index directly by function's return value? If so, Why did it worked at my localhost, but not at my live host?

2
  • It should work in 5.4 Commented Apr 29, 2013 at 7:32
  • yeah my hoster is at php 5.3 - that explains it all and one hour wasted time :/ thanks Commented Apr 29, 2013 at 7:38

2 Answers 2

4

$pageUrl = explode('?',curPageURL())[0]; only available when php version >= 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.

Your online host is below that version.

Sign up to request clarification or add additional context in comments.

Comments

4

You would need current() until you have PHP 5.4 that supports array dereferencing on function results.

$pageUrl = current(explode('?',curPageURL()));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.