且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP中的变量变量

更新时间:2023-11-24 23:46:28

  • $hash('foo')是变量函数.
    $hash可能包含带有函数名称的字符串或匿名函数.

    • $hash('foo') is a variable function.
      $hash may contain a string with the function name, or an anonymous function.

      $hash = 'md5';
      
      // This means echo md5('foo');
      // Output: acbd18db4cc2f85cedef654fccc4a4d8
      echo $hash('foo');
      

    • $$foo是变量变量.
      $foo可能包含带有变量名称的字符串.

    • $$foo is a variable variable.
      $foo may contain a string with the variable name.

      $foo = 'bar';
      $bar = 'baz';
      
      // This means echo $bar;
      // Output: baz
      echo $$foo;
      

    • $bar[$foo]是可变数组键.
      $foo可能包含任何可用作数组键的内容,例如数字索引或关联名称.

    • $bar[$foo] is a variable array key.
      $foo may contain anything that can be used as an array key, like a numeric index or an associative name.

      $bar = array('first' => 'A', 'second' => 'B', 'third' => 'C');
      $foo = 'first';
      
      // This tells PHP to look for the value of key 'first'
      // Output: A
      echo $bar[$foo];
      

    • PHP手册上有一篇有关变量变量的文章,以及关于匿名函数的文章(但我没有在上面显示示例后者).

      The PHP manual has an article on variable variables, and an article on anonymous functions (but I didn't show an example above for the latter).