Questions about echo out of string

Source: Internet
Author: User
If the string has a variable, you need to output the value of the variable.


  
   

还有一种方式:

$a=' hello';echo "my name is {$a}";

有时候在动态使用变量名的时候也会有这两种方式:
$this->{$property} = $argument或者$this->$property
这两种效果都是一样的,但是到底有啥区别呢?

Reply content:

If the string has a variable, you need to output the value of the variable.


  
    

还有一种方式:

$a=' hello';echo "my name is {$a}";

有时候在动态使用变量名的时候也会有这两种方式:
$this->{$property} = $argument或者$this->$property
这两种效果都是一样的,但是到底有啥区别呢?

There is no big difference, with curly braces just handy for PHP parsing when you look for variables. For example, you can try:

$a = "hello";echo "$aworld";echo "{$a}world";

That is, when a variable name is the previous part of another variable name, add {} to get what you want.

$a='hello ';$aword=' word';echo "my name  is $aword";  //my name  is wordecho "
";echo "my name is {$a}word"; //my name is hello word

Curly braces {} can only be used in double quotes when using echo

Similar to bash, differentiate variables and strings

//只是为了区别变量而已,如下:$a = "he";//如果这样,你的变量$allo是不存在的echo "$allo";//所以,需要加一个定界符来加区分echo "{$a}llo";//echo  hello//这种情况只在双引号后中有用。如果你需要打印的变量前面没有英文或容易混淆变量的情况下可以不用的。
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.