dongmei6426 2018-06-11 04:30
浏览 42
已采纳

在PHP中使用带有变量名的GET和POST数组

I'm struggling with variable variables in PHP. I have the following piece of code.

if($_GET["action"] == 1){
        $action = "_GET";
    }else{
        $action = "_POST";
}
...
${$action["lang_id"]}

I get the following errors:

  • Illegal string offset "lang_id" (but if I use directly the $_GET name it works properly)
  • udefined variable _ (yes, an underscore).

thanks in advance

  • 写回答

2条回答 默认 最新

  • drau89457 2018-06-11 04:33
    关注
    ${$action["lang_id"]}
    

    Remember that the variable name goes between ${ and }, and that the variable name you are looking for is _GET or _POST.

    You are trying to get the value of $action["lang_id"] and use that as the variable name… but $action is a string.

    You need to use $action as the variable name and then read the property from the result.

    ${$action}["lang_id"]
    

    … but you would probably be better off using $_REQUEST instead of variable variables. (Watch out for cookies with the same name as post or query string data though).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部