How to use variable inside parameter $_GET? example: ($_GET[$my_var])

半腔热情 提交于 2019-12-02 14:24:53

问题


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]


来源:https://stackoverflow.com/questions/19126129/how-to-use-variable-inside-parameter-get-example-getmy-var

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!