I will try to explain the solution I posted earlier in as much detail as I can.
First, here is the solution again.
Code: Select all
parse_str(parse_url($url, PHP_URL_QUERY), $vars);
- $url is a variable that holds a string. It is the URL that comes from your database. For example, "http://localhost/phpmyadmin/site/index. ... =&id4=&id5".
- PHP_URL_QUERY is a constant that will affect the parse_url() function. The actual value of the constant does not matter to you, the programmer, but it does matter to parse_url().
- parse_url is the name of the function that will be called/executed first.
- $vars is a new, empty variable that will later hold the values we are interested in. Right now, it holds nothing.
- parse_str is the name of the function that will be called/executed second.
Now, use your imaginary magnifying glass. This is the function call that executes parse_url().
- $url and PHP_URL_QUERY are arguments being passed to the function.
- Because PHP_URL_QUERY is passed to it, parse_url() knows that it should return the query part of the given URL. So, for example, the function call is replaced with the string "page=selector&menu=home&id1=397&id2=13&id3=&id4=&id5"
Discard the magnifying glass. After parse_url() has returned a value, the original line might now look like this. This next function call executes parse_str().
Code: Select all
parse_str("page=selector&menu=home&id1=397&id2=13&id3=&id4=&id5", $vars);
- The string "page=selector&menu=home&id1=397&id2=13&id3=&id4=&id5" is what was returned by parse_url(). It is now an argument being passed to parse_str().
- $vars is an empty variable that is set for the first time on this line. As the second parameter of parse_str(), $vars will be passed by reference (rather than by value as $url and PHP_URL_QUERY were to parse_url()). That means that $vars can be changed by parse_str() and the change will remain after parse_str() is done.
- The return value of parse_str() is not useful (it is always NULL), so there is no point echoing it or capturing it with a variable. Instead, if not given a second argument, parse_str() will create variables in the current scope that have the same names as those in the given query string. However, if given a variable as the second argument (like $vars), parse_str() will instead make that variable hold an array, and fill the array with the values from the query string. The array will be associative with names from the query string as the keys.
Just for consistency, I'll show that the parse_str() function call has now been reduced to, and replaced with, this.
However, $vars was passed by reference, so it could be considered that the function call is replaced with this.
Code: Select all
$vars = array(
'page' => 'selector',
'menu' => 'home',
'id1' => '397',
'id2' => '13',
'id3' => '',
'id4' => '',
'id5' => ''
);
To see what is in the array that $vars holds, use var_dump() or print_r().
- Both print_r() and var_dump() create a string representation of the given argument and send the string directly to the output stream (most likely over HTTP to your browser window).
Calling print_r() on $vars is equivalent to this.
Code: Select all
echo "Array
(
[page] => selector
[menu] => home
[id1] => 397
[id2] => 13
[id3] =>
[id4] =>
[id5] =>
)
";
Now, you can get values from the array with array syntax. For example, to get the value "397" at key "id1",
And to see that value,
Code: Select all
var_dump($vars['id1']); // string(3) "397"
All of this (and more) can be learned from the manual.