Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
It should work in 5.4 –  Ja͢ck Apr 29 '13 at 7:32
    
yeah my hoster is at php 5.3 - that explains it all and one hour wasted time :/ thanks –  abimelex Apr 29 '13 at 7:38

2 Answers 2

up vote 4 down vote accepted

$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.

share|improve this answer

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

$pageUrl = current(explode('?',curPageURL()));
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.