简体   繁体   English

$ var和$ {var}之间有什么区别吗

[英]Is there any differences between $var and ${var}

Are these 2 two "spellings" equivalent? 这两个“拼写”相等吗? Just wondering. 就是想。

${var} out of context could be either correct or not. 超出上下文的${var}可能是正确的,也可能是错误的。 If it is used inside of the string like "foo ${var} bar" - then it is the same. 如果在"foo ${var} bar"之类的字符串中使用它,则它是相同的。

If it is used right in the code - then ${var} is incorrect, and ${'var'} should be used instead. 如果在代码中正确使用它-那么${var}是不正确的,应改用${'var'}

The valid cases for using ${...} are: 使用${...}的有效情况是:

  1. Inside the string in cases like "ab${cd}e" - when all the letters go without spaces, "${a['b']}" - when you use it with arrays 在字符串中,例如"ab${cd}e" -当所有字母都没有空格时, "${a['b']}" -与数组一起使用时

  2. When you want to assemble the variable name dynamically: ${'a_' . $i} 当您要动态汇编变量名时: ${'a_' . $i} ${'a_' . $i}

Pretty much. 差不多了 The only difference is that you can enter code to be parsed in between the curly braces to get "variable" variable names. 唯一的区别是,您可以输入要在花括号之间进行解析的代码,以获取“变量”变量名。

Ex. 例如

${'t'.'e'.'s'.'t'} = 'test'; // is the same as $test = 'test';  
${substr('testaaa',0,4)} = 'test'; // the same

You can even do something like: 您甚至可以执行以下操作:

${ 'a' == 'b' ? 'Foo' : 'test' } = 'test'; //the same

It is essentially the same as: 它基本上与以下内容相同:

$var_name = substr('testaaa',0,4);
$$var_name = 'test';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM