【问题标题】:validation with variable variables not working变量变量的验证不起作用
【发布时间】:2009-06-21 02:28:09
【问题描述】:
$method = 'post';

$method = strtoupper($method);
echo $method.'test1';

$method = '_'.$method;
echo $method.'test2';

$method = $$method;
echo $method.'test3';

为什么不打印 $_POST 2 到 3 之间的内容?

【问题讨论】:

    标签: php variables validation


    【解决方案1】:

    您希望$method['test3'] 访问$_POST 数组的元素。点. 运算符执行字符串连接。方括号[] 用于数组访问。

    【讨论】:

    • 不,我想要来自 'session'、'cookie'、'post' 和 'get' 的 $_SESSION、$_COOKIE、$_POST 和 $_GET。
    • 我想我最终会使用 if($method == 'post') $method = $_POST; if($method == 'get') $method = $_GET; if($method == 'session') $method = $_SESSION; if($method == 'cookie') $method = $_COOKIE;
    【解决方案2】:

    除了 John Kugelman 的优秀观点之外,我会使用以下内容

    $method = $_POST;
    
    echo $method['test1'];
    
    echo $method['test2'];
    
    echo $method['test3'];
    

    不必费心尝试通过字符串访问 contant 数组名称

    如果你真的坚持使用字符串来访问这些,你可以

    $method = "post";
    $method = strtoupper($method."_");    
    if (isset(${$method})) {
      $method = ${$method};
    
      echo $method['test1'];
    
      echo $method['test2'];
    
      echo $method['test3'];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多