How to use variable inside parameter $_GET? exampl

2019-03-03 18:59发布

问题:

I'm developing a plugin for wordpress, the parameter of the $ _GET is recorded in the database according to the preference of the User via the Wordpress Admin Panel. The following validation has to be via the $ _GET, this is the function:

$db_url = get_option('my_get_url');
// returns the value of the database entered by User
// on this case return -->  page=nosupport

$url_explode = explode("=", $db_url);
$url_before = $url_explode[0]; // --> page
$url_after = $url_explode[1]; // --> nosupport

echo "Before: ".$url_before; // here are ok, return --> page
echo "After: ".$url_after; // here are ok, return --> nosupport

My problem is here:

// here $_GET no have any value, dont work on validate...
if($_GET[$url_before] != ""){ 
    if($_GET['$url_before']=="nosupport"){
        // my function goes here...
    }
}

I using for test the parameter:

echo $_GET[$url_before];

But dont return any value...

回答1:

I found the problem, i had already tested all of these options, but ever dont working, the problem was that I was testing the function inside the main page of my site, and on the main page (mysite.com) does not get the parameter (?page=nossuport), so always returning null values​​, when I used the variable in the GET or used the echo $GET[$my_var] to test.. It was a great carelessness of mine, would never work...

by the way, the two parameters works correctly:

$_GET[$url_before]
$_GET["$url_before"]

The Problem are solved, Thanks for help.



回答2:

if($_GET[$url_before] != ""){ 
    if($_GET[$url_before]=="nosupport"){ // note no "" here
        // my function goes here...
    }
}

In your solution, the key was treated as a string, with no variables evaluated.



回答3:

You forgot to take out the ' in the second condition.

You wrote:

     $_GET['$url_before']

I'm guessing it should be:

     $_GET[$url_before]