php 何时用花括号将变量括起来
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35598187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to wrap curly braces around a variable
提问by Red Virus
I don't know how to explain this but in simple terms I have seen people using {$variable}
when outputting values. I have noticed that {$variable}
doesn't work everything. When should we use {$variable}
?
我不知道如何解释这一点,但简单地说,我看到人们{$variable}
在输出值时使用。我注意到这{$variable}
并不能解决所有问题。我们应该什么时候使用{$variable}
?
回答by Fakhruddin Ujjainwala
What are PHP curly braces:
什么是 PHP 花括号:
You know that a string can be specified in four different ways. Two of these ways are – double quote("") and heredoc syntax. You can define a variable in those 2 types of strings and PHP interpreter will parse or interpret that variable too, within the strings.
您知道可以通过四种不同的方式指定字符串。其中两种方式是——双引号("")和heredoc语法。您可以在这两种类型的字符串中定义一个变量,PHP 解释器也会在字符串中解析或解释该变量。
Now, there are two ways you can define a variable in a string – simple syntax which is the most used method of defining variables inside a string and complex syntax which uses curly braces to define variables.
现在,您可以通过两种方式在字符串中定义变量 - 简单语法是在字符串中定义变量的最常用方法,以及使用花括号定义变量的复杂语法。
Curly braces syntax:
花括号语法:
To use a variable with curly braces is very easy. Just wrap the variable with { and } like:
使用带花括号的变量非常容易。只需用 { 和 } 包装变量,例如:
{$variable_name}
{$variable_name}
Note: There must not be any gap between { and $. Else, PHP interpreter won't consider the string after $ as a variable.
注意:{ 和 $ 之间不能有任何间隙。否则,PHP 解释器不会将 $ 后面的字符串视为变量。
Curly braces example:
花括号示例:
<?php
$lang = "PHP";
echo "You are learning to use curly braces in {$lang}.";
?>
Output:
输出:
You are learning to use curly braces in PHP.
When to use curly braces:
何时使用花括号:
When you are defining a variable inside a string, PHP might mix up the variable with other characters if using simple syntax to define a variable and this will produce an error. See the example below:
当您在字符串中定义变量时,如果使用简单的语法来定义变量,PHP 可能会将变量与其他字符混淆,这将产生错误。请参阅下面的示例:
<?php
$var = "way";
echo "Two $vars to defining variable in a string.";
?>
Output:
输出:
Notice: Undefined variable: vars …
In the above example, PHP's interpreter considers $vars a variable, but, the variable is $var. To separate a variable name and the other characters inside a string, you can use curly braces. Now, see the above example using curly braces-
在上面的例子中,PHP 的解释器认为 $vars 是一个变量,但是,这个变量是 $var。要将变量名和字符串中的其他字符分开,可以使用花括号。现在,请参阅上面使用花括号的示例-
<?php
$var = "way";
echo "Two {$var}s to define a variable in a string.";
?>
Output:
输出:
Two ways to define a variable in a string.
Source: http://schoolsofweb.com/php-curly-braces-how-and-when-to-use-it/
来源:http: //schoolsofweb.com/php-curly-braces-how-and-when-to-use-it/
回答by Sanjok Gurung
A couple of years late but may I add:
晚了几年,但我可以补充一下:
You can even use variable in curly braces to access methods of an Object from a Class dynamically.
您甚至可以使用花括号中的变量来动态访问类中的对象方法。
Example:
例子:
$username_method = 'username';
$realname_method = 'realname';
$username = $user->{$username_method}; // $user->username;
$name = $user->{$realname_method}; // $user->realname
Not a good example but to demonstrate the functionality.
不是一个很好的例子,而是为了演示功能。
Another Example as per @kapreski's request in the comments.
根据@kapreski 在评论中的请求的另一个示例。
/**Lets say you need to get some details about the user and store in an
array for whatever reason.
Make an array of what properties you need to insert.
The following would make sense if the properties was massive. Assume it is
**/
$user = $this->getUser(); //Fetching User object
$userProp = array('uid','username','realname','address','email','age');
$userDetails = array();
foreach($userProp as $key => $property) {
$userDetails[] = $user->{$property};
}
print_r($userDetails);
Once the loop completes you will see records fetched from user object in your $userDetails
array.
循环完成后,您将看到从$userDetails
数组中的用户对象获取的记录。
Tested on php 5.6
在 php 5.6 上测试
回答by Amr Mahmoud Ezzat
As far as I know, you can use for variable $x
据我所知,您可以使用 for 变量 $x
echo "this is my variable value : $x dollars";
… but if you don't have any spaces between the variable and the text around it, you should use {}
. For example:
...但如果变量与其周围的文本之间没有任何空格,则应使用{}
. 例如:
echo "this is my variable:{$x}dollars";
because if you wrote it $xdollars
it will interpret it as another variable.
因为如果你写它,$xdollars
它会将它解释为另一个变量。