note 100891 deleted from control-structures.foreach by googleguy
| From: | googleguy@php.net | Date: | Wed, 14 Nov 2012 22:24:12 +0000 |
| Subject: | note 100891 deleted from control-structures.foreach by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-191953@lists.php.net to get a copy of this message | ||
Note Submitter: Sipos Tams
----
This is an example of the use of the second, minor case of foreach.
A useful script that returns the relative URL of the current page with the current "GET
data".
<?php
function getCurrentURL() {
$currentURL = basename($_SERVER["PHP_SELF"]);
$i = 0;
foreach($_GET as $key => $value) {
$i++;
if($i == 1) { $currentURL .= "?"; }
else { $currentURL .= "&"; }
$currentURL .= $key."=".$value;
}
return $currentURL;
}
?>
Note that most of the time basename($_SERVER["REQUEST_URI"] is enough for this task as
well, but if in the browsers's address bar is only http://example.com/ instead of http://example.com/index.php displayed, than
basename($_SERVER["REQUEST_URI"] fails.