dongmei6426 2018-06-11 12: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 12: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条)

报告相同问题?

悬赏问题

  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符