Heredoc and nowdoc in php define strings _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags php define
In php, heredoc and nowdoc are two defined strings. [Php]? Php is troublesome when it needs to write a large text segment with many lines containing single quotation marks and double quotation marks, leading to escape. you can also use heredoc and nowdoc to define the string's [php]

// When you need to write a large text segment with many lines containing single quotation marks and double quotation marks, it is troublesome to escape.

// You can also use heredoc or nowdoc to define strings.

$ Age = 29;

$ Str = <

Let me write a line,

Line 2 ,'"\,

Dsafd abc \ t \ r \ n haha

$ Age

Cont;

Echo $ str;

/**

Heredoc

< <标识符 < p>

Large text in the middle

Identifier;

Note:

1: The identifier name must be the same as the variable name.

2: The identifier of heredoc. it must be an exclusive line without any other characters.

3: heredoc can parse text like double quotation marks (\ r \ n \ t), variables, and so on.

**/

// Heredoc is easy to write large text segments, but it parses internal characters like double quotation marks.

// If I don't want this, is there a large text like heredoc,

// However, parsing text is as simple as single quotes.

// Is there any such usage?

// A: www.2cto.com is available.

// After 5.3.0, the nowdoc method is added to achieve the above effect.

Echo'

';

$ Str = <'cont'

Let me write a line,

Line 2 ,'"\,

Dsafd abc \ t \ r \ n haha

$ Age

Cont;

Echo $ str;

/***

The nowdoc writing method is the same as heredoc, but the identifiers are enclosed in single quotes,

In this way, the parsing of large text is the same as that of single quotes.

Do not escape \ n \ r \ t, variables, etc.

***/

?>

Why? Php // when you need to write a large text segment with many lines containing single quotation marks and double quotation marks, it is troublesome to escape the text. // You can also use heredoc and nowdoc to define the string...

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.