Php weird behavior, array access

I have a function that returns an array called curPageURL. On my local apache, I turned to the return value of the page, like this: $pageUrl = explode('?',curPageURL())[0]; he worked very well. But it didn’t work live. It took me a long time to figure out that there was an error accessing the array.

This solved the problem:

 $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 someone explain why?

  • Is it forbidden to access the index of an array directly using the return value of a function? If so, why did it work on my localhost, but not on my hosting ?

+6
source share
2 answers

$pageUrl = explode('?',curPageURL())[0]; available only in php version> = 5.4

According to PHP 5.4, you can mass dereference a result by calling a function or method. Before this was only possible using a temporary variable.

Your internet host is below this version.

+4
source

You will need current() until you have PHP 5.4, which supports dereferencing the array of function results .

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

Source: https://habr.com/ru/post/943863/


All Articles