何时在变量周围缠绕花括号

时间:2022-08-26 22:35:01

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}?

2 个解决方案

#1


40  

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}

{$变量名}

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/

#2


7  

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.

您甚至可以在花括号中使用变量来动态地从类中访问对象的方法。

For eg

例如

$usernamemethod = 'username';
$realnamemethod = 'realname';

$username = $user->{$usernamemethod}; // $user->username;
$name = $user->{$realnamemethod}; // $user->realname

Not a good example but to demonstrate the functionality.

不是一个很好的例子,只是为了演示功能。

Tested on php 5.6

在PHP 5.6上测试过

#1


40  

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}

{$变量名}

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/

#2


7  

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.

您甚至可以在花括号中使用变量来动态地从类中访问对象的方法。

For eg

例如

$usernamemethod = 'username';
$realnamemethod = 'realname';

$username = $user->{$usernamemethod}; // $user->username;
$name = $user->{$realnamemethod}; // $user->realname

Not a good example but to demonstrate the functionality.

不是一个很好的例子,只是为了演示功能。

Tested on php 5.6

在PHP 5.6上测试过