The third way to define a string is to use the 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.
Warning
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;
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
You can also use the HEREDOC structure in function parameters to transfer data:
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;
?>
Note:
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.