<< in PHP <是什么符号
Reply to discussion (solution)
Delimiter Echo <<
My name is "$name". I am Printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital ' a ': \x41
EOT;
?>
Another way to set the bounds of a string is to use the delimiter syntax ("<<<"). You should provide an identifier after <<<, followed by a string, followed by the same identifier end string.
Delimiter
Another way to set the bounds of a string is to use the delimiter syntax ("<<<"). You should provide an identifier after <<<, followed by a string, followed by the same identifier end string.
The end identifier must start at the first column of the row. Similarly, identifiers must follow the naming conventions of any other tag in PHP: They can only contain alphanumeric underscores, and must begin with an underscore or non-numeric character.
HEREDOC syntactic structure:<<<. After the prompt, you define an identifier and then a new line. The next is the string itself, and finally the identifier defined earlier as the end flag.
The identifier referenced at the end must be at the beginning of a line, and the name of the identifier should follow the rules of PHP like any other label: it can only contain letters, numbers, and underscores, and cannot begin with numbers and underscores.
Note that the end identifier line may not include any other characters except for a semicolon (;). This means that the identifier cannot be indented, and the semicolon cannot have any whitespace or tabs before or after it. More importantly, the end identifier must be preceded by a new row label recognized by the local operating system, such as in Unix and Mac OS x systems, and the end identifier (possibly a semicolon) followed by a new row label.
If you do not follow this rule and the end tag is not "clean", PHP will assume that it is not the end identifier and continue to look for it. If a correct end identifier is not found before the end of the file, PHP will produce a syntax error on the last line.
The HEREDOCS structure cannot be used to initialize class, and after PHP 5.3, the limit can only be used in cases where a variable is included.
Example #1 Illegal Example
class Foo {
Public $bar = <<
Bar
EOT;
}
?>
The HEREDOC structure is like a double-quote string that does not use double quotes, which means that the quotes are not replaced in the HEREDOC structure, but the characters listed above (\ n, etc.) are also available. The variables will be replaced, but be careful when the strings express complex variables in the HEREDOC structure.
Example Example of string #2 heredoc structure
$str = <<
Example of String
Spanning multiple lines
Using Heredoc syntax.
EOD;
/* More complex examples with variables */
Class Foo
{
var $foo;
var $bar;
function foo ()
{
$this->foo = ' foo ';
$this->bar = Array (' Bar1 ', ' Bar2 ', ' Bar3 ');
}
}
$foo = new Foo ();
$name = ' MyName ';
Echo <<
My name is "$name". I am Printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital ' a ': \x41
EOT;
?>
The above routines will output:
My name is "MyName". I am printing some Foo.
Now, I am printing some Bar2.
This should print a capital ' a ': A can also be used to transfer data using the HEREDOC structure in function parameters:
Example of Example #3 heredoc structure in Parameters
Var_dump (Array (<<
foobar!
EOD
));
?>
After PHP 5.3.0, you can also use the HEREDOC structure to initialize the properties and constants of static variables and classes:
Example #4 Use the HEREDOC structure to initialize static values
static variables
function foo ()
{
Static $bar = <<
Nothing is here ...
LABEL;
}
Constants, properties of the class
Class Foo
{
Const BAR = <<
Constant Example
FOOBAR;
Public $baz = <<
Property Example
FOOBAR;
}
?>
In PHP 5.3.0, you also declare a marker in the HEREDOC structure with double quotation marks:
Example #5 use double quotes in the HEREDOC structure
echo <<< "FOOBAR"
Hello world!
FOOBAR;
?>
Delimiter
The function of the 1.PHP delimiter is to follow the original, including the newline format, and output something inside it;
2. Any special characters in the PHP delimiter do not need to be escaped;
The PHP variable in the 3.PHP delimiter is replaced with its value as normal.
This can be useful for storing large sections of text.