PHP inside string operation, use {} method

xiaoxiao2021-03-06  20

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.

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.

'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:

转载请注明原文地址:https://www.9cbs.com/read-44055.html

New Post(0)