gpt4 book ai didi

php - PHP 中的可变变量

转载 作者:可可西里 更新时间:2023-11-01 13:36:03 25 4
gpt4 key购买 nike

我知道你可以这样做:$hash('foo')$$foo 以及 $bar[$foo],这些东西分别叫什么?

最佳答案

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

    $hash = 'md5';

    // This means echo md5('foo');
    // Output: acbd18db4cc2f85cedef654fccc4a4d8
    echo $hash('foo');
  • $$foo 是可变变量。
    $foo 可能包含带有变量名称的字符串。

    $foo = 'bar';
    $bar = 'baz';

    // This means echo $bar;
    // Output: baz
    echo $$foo;
  • $bar[$foo] 是可变数组键。
    $foo 可以包含任何可用作数组键的内容,例如数字索引或关联名称。

    $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 手册中有一篇关于 variable variables 的文章,以及关于 anonymous functions 的文章(但我没有在上面展示后者的示例)。

关于php - PHP 中的可变变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3645361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com