${$item} 是什么意思?

发布于 2024-11-26 07:21:16 字数 300 浏览 1 评论 0原文

在下面的情况下,我无法理解大括号的目标,并且我没有找到有关大括号使用的严肃文档。

请参阅下面的示例:

 $var = array('a','b','c','d');

 foreach($var as $item){

       ${$item} = array();

 }

我不理解 ${$item} 的含义。

我在 foreach 循环之前和之后尝试过 var_dump 但似乎没有任何反应。

有什么想法吗?

I'm not able to understand braces goal in the situation below and I find no serious documentation about braces usage.

See the example below:

 $var = array('a','b','c','d');

 foreach($var as $item){

       ${$item} = array();

 }

I'm non understanding ${$item} meaning.

I've tried with var_dump before and after foreach loop but it seems nothing happens.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

混浊又暗下来 2024-12-03 07:21:16

它创建 4 个空数组:

$a, $b, $c, $d // arrays now

It creates 4 empty arrays:

$a, $b, $c, $d // arrays now
夕嗳→ 2024-12-03 07:21:16

大括号创建一个与大括号内提供的字符串同名的变量。
在您的代码中,它通过从数组中获取字符串来创建 4 个新变量 $a、$b、$c、$d
$var 。

下面是一个示例,用于查看代码中创建的变量的差异:
http://codepad.org/E2619ufe

<?php

$var = array('a','b','c','d');
$currentState = get_defined_vars();

foreach($var as $item){

       ${$item} = array();

 }

$newState =  get_defined_vars();
$newVariables = array_diff(array_keys($newState),array_keys($currentState));
var_dump($newVariables);

?>

以下是大括号的用法示例:
http://codepad.org/KeE75iNP

<?php

${'myVar'} = 12345;
var_dump($myVar);

/* also helpful when the variable name contains $ due to some reason */

${'theCurrency
} = 4000;
var_dump(${'theCurrency
});

/* uncomment line below, it will raise syntax error */
//var_dump($theCurrency$); 


?>

Curly braces create a variable with same name as string provided inside the curly braces.
In your code, it creates 4 new variables $a,$b,$c,$d by taking strings from the array
$var .

Here is an example to see the difference in variables created in your code:
http://codepad.org/E2619ufe

<?php

$var = array('a','b','c','d');
$currentState = get_defined_vars();

foreach($var as $item){

       ${$item} = array();

 }

$newState =  get_defined_vars();
$newVariables = array_diff(array_keys($newState),array_keys($currentState));
var_dump($newVariables);

?>

Here is an example usage of curly braces:
http://codepad.org/KeE75iNP

<?php

${'myVar'} = 12345;
var_dump($myVar);

/* also helpful when the variable name contains $ due to some reason */

${'theCurrency
} = 4000;
var_dump(${'theCurrency
});

/* uncomment line below, it will raise syntax error */
//var_dump($theCurrency$); 


?>
凉栀 2024-12-03 07:21:16

是的,它确实创建了 4 个空数组,您正在运行时创建变量,这就是大括号的用法。以下是大括号的用法示例: php 中的大括号

yes exactly it creates 4 empty arrays , you are creating variables on runtime , thats the usage of braces . here is examples of the usage of braces : braces in php

今天小雨转甜 2024-12-03 07:21:16

花括号中放入的任何内容都将替换变量的值。

所以最终值将是 4 个空数组

${item} will become $a, ie: $a = array();
${item} will become $b, ie: $b = array();
${item} will become $c, ie: $c = array();
${item} will become $d, ie: $d = array();

Anything you put in curly brace will substitute value of the variable.

So the end value will be 4 empty arrays

${item} will become $a, ie: $a = array();
${item} will become $b, ie: $b = array();
${item} will become $c, ie: $c = array();
${item} will become $d, ie: $d = array();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文