When a string is specified with a double quotation or a delimiter, the variables are parsed.
There are two grammar, a simple and one complicated. The simple syntax is most common and convenient, it provides a method of analyzing variables, array values, or object properties.
The complex syntax is introduced by PHP 4, and an expression can be enclosed in the curneth.
Simple syntax
If you encounter your dollar symbol ($), the parser will take the following characters as much as possible to form a legal variable name. If you want to express the end of the specified name, create a variable with the curneth.
php $ beer = 'Heineken'; echo "$ beer's taste is great"; // Works, "'" is an invalid character for varnamesecho "He Drank Some $ beers"; // Won't work,' s' Is a valid character for varnamesecho "He Drank Some $ {beer} s"; // Worksecho "He Drank Some {$ beer} S"; // Works?>
You can also analyze the array index or object properties. For array indexes, right brackets (]) marks the end of the index. Object properties and simple variables apply the same rule, although there is no small trick like variables for object properties.
Php // These examples are specific to using arrays inside of strings.// When outside of a string, always quote your array string keys // and do not use {braces} when outside of strings either.// Let's show all errorserror_reporting (E_ALL); $ fruits = array ( 'strawberry' => 'red', 'banana' => 'yellow'); // Works but note that this works differently outside string-quotesecho "A banana is $ fruits [banana ]. "; // Worksecho" a banana is {$ fruits ['banana']}. "; // Works But PHP Looks for a constant named banana first // as described Below.echo" a banana is {$ fruits [ Banana]}. "// Won't work, use baraces. this results in a parse error.echo" a banana is $ fruits ['banana']. "// Worksecho" a banana is ". $ fruits [ 'Banana']. "; // Worksecho" Square is $ Square-> width meters Broad. "; // Won't work. for a solution, see the complex syntax.echo" this Square is $ Square- > Width00 Centimeters Broad. ";> For any more complex case, complex syntax should be used.
Complex (curly bracket) grammar
Not because the grammar is complicated, it is complicated, but because this method can contain complex expressions.
In fact, use this syntax you can contain any value in the namespace in the string. Use only to write an expression in the same way outside the string, then contain it in {and}. Because you can't escape "{", this syntax is only recognized when $ keep back ("{/ $" or "{$" is recognized ("{$")). Use some examples to be clearer: