note 100891 modified in control-structures.foreach by danbrown

From: Date: Sat, 13 Nov 2010 15:07:53 +0000
Subject: note 100891 modified in control-structures.foreach by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-173996@lists.php.net to get a copy of this message
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 .= "&amp;"; } $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. --was-- 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". function getCurrentURL() { $currentURL = basename($_SERVER["PHP_SELF"]); $i = 0; foreach($_GET as $key => $value) { $i++; if($i == 1) { $currentURL .= "?"; } else { $currentURL .= "&amp;"; } $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://something.com/ instead of http://something.com/index.php displayed, than basename($_SERVER["REQUEST_URI"] fails. http://php.net/manual/en/control-structures.foreach.php

« previous php.notes (#173996) next »