What is the <symbol in php? how can this problem be solved?

Source: Internet
Author: User
& Lt; what is the symbol in php & lt; what is the symbol ------ solution ------------------ define the symbol echo & lt; & lt; EOTMynameis & quot; $ name & quot ;. iamprintingsome $ foo-& gt; foo. now, Iamprinti php < <是什么符号
Php < <是什么符号

------ Solution --------------------
Separator echo <
My name is "$ name". I am printing some $ foo-> foo.
Now, I am printing some {$ foo-> bar [1]}.
This shoshould print a capital 'A': \ x41
EOT;
?>

------ Solution --------------------
Delimiters
Another method is to define the string using the delimiter syntax ("<"). An identifier should be provided after <, followed by a string, followed by the end string of the same identifier.

The end identifier must start from the first column of the row. Similarly, the identifier must follow the naming rules for any other tag in PHP: it can only contain letters, numbers, underscores (_), and must start with an underscore or a non-digit character.
------ Solution --------------------
Heredoc syntax: <. At the end of the prompt, you need to define an identifier and then a new line. The next step is the string itself. Finally, use the identifier defined above as the end mark.

The identifier referenced at the end must be at the beginning of a row. In addition, the name of the identifier must follow the PHP rules like other labels: it can only contain letters, numbers, and underscores, it cannot start with a number or underline.
Note that the end identifier line cannot contain any other character except a semicolon. This means that the identifier cannot be indented, and there cannot be any blank or tabs before and after the semicolon. More importantly, the end identifier must be preceded by a New Line label recognized by the local operating system, such as \ n in UNIX and Mac OS X systems, the end identifier (which may have a semicolon) must be followed by a New Line label.

If the end tag is not "clean" due to non-compliance with this rule, PHP considers it not the end identifier and continues searching. If a correct end identifier is not found before the end of the file, PHP will generate a syntax error in the last line.

The Heredocs structure cannot be used to initialize the class, but after PHP 5.3, this restriction can only be used to include variables.

Example #1 invalid Example

Class foo {
Public $ bar = <
Bar
EOT;
}
?>
The Heredoc structure is like a double quotation mark string without double quotation marks. This means that quotation marks are not replaced in the heredoc structure, but the characters listed above (\ n, etc.) can also be used. The variable will be replaced, but be especially careful when strings in the heredoc structure express complex variables.

Example #2 string Example of the 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 shoshould print a capital 'A': \ x41
EOT;
?>
The above routine will output:

My name is "MyName". I am printing some Foo.
Now, I am printing some Bar2.
This shoshould print a capital 'A': A can also use the Heredoc structure to transmit data in function parameters:

Example #3 Example of the Heredoc structure in parameters

Var_dump (array (<
Foobar!
EOD
));
?>
After PHP 5.3.0, you can also use the Heredoc structure to initialize static variables and class attributes and constants:

Example #4 use the Heredoc structure to initialize static values

// Static variables
Function foo ()
{
Static $ bar = <
Nothing in here...
LABEL;
}

// Constants and attributes of a class
Class foo
{
Const BAR = <
Constant example
FOOBAR;

Public $ baz = <
Property example
FOOBAR;
}
?>
In PHP 5.3.0, double quotation marks are also used to declare the identifier in the Heredoc structure:

Example #5 use double quotation marks in the heredoc structure

Echo <"FOOBAR"
Hello World!
FOOBAR;
?>
------ Solution --------------------
Delimiters
1. PHP delimiters act as they are, including the line feed format or something, and output something inside them;
2. do not escape any special characters in the PHP delimiters;
3. the PHP variable in the PHP delimiter will be replaced with its value normally.

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.