在 PHP 中,“<<<”是什么意思?代表?

发布于 2024-09-18 11:56:17 字数 67 浏览 11 评论 0 原文

例如:

$sql = <<<MySQL_QUERY

For example:

$sql = <<<MySQL_QUERY

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

雪化雨蝶 2024-09-25 11:56:18

我发现 HeredocNowdocPHP 中都非常强大和有用,令我惊讶的是,迄今为止没有人提供更多示例来说明您可以做什么做。

首先,HeredocNowdoc 之间的区别很简单,

  • Heredoc:就像“”双引号字符串一样,您可以放入变量
  • Nowdoc:就像 '' 单引号字符串没有解析变量

对于以下示例,我将仅显示 Heredoc,按顺序要制作 Nowdoc 只需将标记括在单引号内 -> “令牌”。

特点和优点

  • "" 和 '' 可以根据需要添加,不会导致任何错误
  • 轻松输出带有动态变量的 HTML 代码,避免使用串联。
  • 将其存储在变量中以供字母使用,可以创建小组件并直接输出它们。
  • 这些行按字面意思解释为 '\n' 因此就像在文档中写入一样,用 nl2br 添加
    也很有用。

简单示例

$a = "Hello";
$b = "World";
// HEREDOC
echo <<<HEREDOC
<strong> HEREDOC:  </strong> 
Variable A: "$a" 
Variable B: "$b"
HEREDOC;
echo '</br>';

// NOWDOC
echo <<<'NOWDOC'
<strong> NOWDOC:  </strong> 
Variable A: "$a" 
Variable B: "$b"
NOWDOC;

输出

HEREDOC: Variable A: "Hello" Variable B: "World"
NOWDOC: Variable A: "$a" Variable B: "$b"

配方

  1. 使用 nl2br 为每行添加

这有效,因为 HEREDOC将每个 \n 解释为实际行

   // HEREDOC
    echo nl2br(<<<HEREDOC
    <strong> HEREDOC:  </strong> 
    Variable A: "$a" 
    Variable B: "$b"
    HEREDOC);
    // Output HEREDOC:
    //Variable A: "Hello"
    //Variable B: "World"
  1. 创建小组件

    <前><代码>

    {$任务['名称']}
    {$task['state']}

    {$task['描述']}

    查看任务待办事项

    赫雷多克;
    回显$组件; // 输出

    }

    ?>

或者只放入一个字符串,然后使用 1 echo

    <?php
        $taskRendered = '';
        foreach($tasks  as $task) {
            // Create an HTML like component
            $component = <<<HEREDOC
            <div class="pure-u-1-3">
                <div class="card">
                    <div class="card-header">
                       {$task['name']}
                    </div>
                    <div class="card-body">
                        <h5 class="card-title"> {$task['state']} </h5>
                        <p class="card-text"> {$task['description']} </p>
                        <a href="view?model=todo_list&task_id={$task['id']}" class="btn btn-primary">See Task Todos</a>
                    </div>
                </div>                    
            </div>
            HEREDOC;
            $taskRendered .= $component;
        }
        echo $taskRendered; // Output
    
    ?>

文档

I found both Heredoc and Nowdoc extremelly powerfull and usefull in PHP and I am surprise that no one have so far give more example of what you can do.

First the difference between Heredoc and Nowdoc is simple,

  • Heredoc: Is like the "" double quote string you can put Variables
  • Nowdoc: Is like the '' single quote string no variable are parsed

For the following example I will only show the Heredoc, in order to make a Nowdoc just wrap the token inside single quotes -> 'TOKEN'.

Features and Advantages

  • The "" and '' can be added as much as needed and won't cause any errror
  • Easily output HTML code with dynamic variables, avoid usesell concatenations.
  • Store it in variables for letter use, can create small components and just output them.
  • The Lines are interpreted literally with '\n' hence is like writing in a doc, also useful to add
    with nl2br .

Simple Example

$a = "Hello";
$b = "World";
// HEREDOC
echo <<<HEREDOC
<strong> HEREDOC:  </strong> 
Variable A: "$a" 
Variable B: "$b"
HEREDOC;
echo '</br>';

// NOWDOC
echo <<<'NOWDOC'
<strong> NOWDOC:  </strong> 
Variable A: "$a" 
Variable B: "$b"
NOWDOC;

output

HEREDOC: Variable A: "Hello" Variable B: "World"
NOWDOC: Variable A: "$a" Variable B: "$b"

Recipes

  1. Use nl2br to add <br> for each line

This works because HEREDOC interpretes each \n as an actual line

   // HEREDOC
    echo nl2br(<<<HEREDOC
    <strong> HEREDOC:  </strong> 
    Variable A: "$a" 
    Variable B: "$b"
    HEREDOC);
    // Output HEREDOC:
    //Variable A: "Hello"
    //Variable B: "World"
  1. Create small components

         <?php
             foreach($tasks  as $task) {
                 // Create an HTML like component
                 $component = <<<HEREDOC
                 <div class="pure-u-1-3">
                     <div class="card">
                         <div class="card-header">
                            {$task['name']}
                         </div>
                         <div class="card-body">
                             <h5 class="card-title"> {$task['state']} </h5>
                             <p class="card-text"> {$task['description']} </p>
                             <a href="view?model=todo_list&task_id={$task['id']}" class="btn btn-primary">See Task Todos</a>
                         </div>
                     </div>                    
                 </div>
                 HEREDOC;
                 echo $component; // Output
    
             }
    
         ?>
    

Or just put in one string then output with 1 echo

    <?php
        $taskRendered = '';
        foreach($tasks  as $task) {
            // Create an HTML like component
            $component = <<<HEREDOC
            <div class="pure-u-1-3">
                <div class="card">
                    <div class="card-header">
                       {$task['name']}
                    </div>
                    <div class="card-body">
                        <h5 class="card-title"> {$task['state']} </h5>
                        <p class="card-text"> {$task['description']} </p>
                        <a href="view?model=todo_list&task_id={$task['id']}" class="btn btn-primary">See Task Todos</a>
                    </div>
                </div>                    
            </div>
            HEREDOC;
            $taskRendered .= $component;
        }
        echo $taskRendered; // Output
    
    ?>

Documentation

多彩岁月 2024-09-25 11:56:18

为了得到一个清晰的想法:

$data = array(
  "Id" => 12345,
  "Cutomer" => "hi",
  "Quantity" => 2,
  "Price" => 45
);

curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));

如果我们使用 <<<

$data = <<<DATA
{
  "Id": 12345,
  "Customer": "John Smith",
  "Quantity": 1,
  "Price": 10.00
}
DATA;

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

结论:如果我们使用第一种方法,我们必须将其转换为 json_encode()需要一些处理。相反,我们可以使用 <<< 运算符来节省一些时间并获得一些干净的代码。 :)

To get a clear idea:

$data = array(
  "Id" => 12345,
  "Cutomer" => "hi",
  "Quantity" => 2,
  "Price" => 45
);

curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));

If we use <<<:

$data = <<<DATA
{
  "Id": 12345,
  "Customer": "John Smith",
  "Quantity": 1,
  "Price": 10.00
}
DATA;

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

Conclusion: If we go with the 1st method we have to convert it into json_encode() which somehow requires some processing. Instead, We can use the <<< operator to save some time and get some clean code. :)

任谁 2024-09-25 11:56:17

这是heredoc 语法。您可以通过放置 <<< 加上您选择的标记来启动heredoc字符串,并通过仅将标记(而不是其他任何东西!)放在新行上来终止它。为方便起见,有一个例外:允许您在结束分隔符后添加一个分号。

例子:

echo <<<HEREDOC
This is a heredoc string.

Newlines and everything else is preserved.
HEREDOC;

That's heredoc syntax. You start a heredoc string by putting <<< plus a token of your choice, and terminate it by putting only the token (and nothing else!) on a new line. As a convenience, there is one exception: you are allowed to add a single semicolon after the end delimiter.

Example:

echo <<<HEREDOC
This is a heredoc string.

Newlines and everything else is preserved.
HEREDOC;
仙气飘飘 2024-09-25 11:56:17

这称为heredoc,它可以让您编写跨越多行的长文本。您可以将 PHP 变量放在那里,它们将替换为值。 CHART 这个词可以是任何东西。只需要在引用文本的开始处和停止处相同即可。

您可以通过附加多个带引号的字符串来完成相同的操作,但是对于像此 HTML 文本这样的扩展文档,这在大多数情况下会更清晰。还有一种称为 nowdoc 的东西,它就像 中的单引号字符串PHP,但它们不允许您在其中使用变量。

This is called a heredoc, and it lets you do a long piece of text that goes over several lines. You can put PHP variables in there and they will replace with the value. The word CHART can be anything. It just needs to be the same to start and stop where the quoted text begins.

You could do the same thing by appending multiple quoted strings, but this is cleaner most of the time for extended documents like this HTML text. There is also something called a nowdoc which is like a single quote string in PHP, but these won't let you use variables inside them.

晨光如昨 2024-09-25 11:56:17

它是使用 HEREDOC 语法。

分隔字符串的第三种方法是定界语法:<<<。

在此运算符之后,提供一个标识符,然后提供一个换行符。接下来是字符串本身,然后再次使用相同的标识符来结束引用。

It is the start of a string that uses the HEREDOC syntax.

A third way to delimit strings is the heredoc syntax: <<<.

After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.

蓝天白云 2024-09-25 11:56:17

这是 PHP 的 heredoc< /代码>

例子:

$sql = <<<MySQL_QUERY
SELECT * 
FROM TAB 
WHERE A = 1 AND B = 2 
MySQL_QUERY;           

It's PHP's heredoc.

Example:

$sql = <<<MySQL_QUERY
SELECT * 
FROM TAB 
WHERE A = 1 AND B = 2 
MySQL_QUERY;           
蹲在坟头点根烟 2024-09-25 11:56:17

这是一个定界符,对于长字符串,您不必担心引号之类的问题。如果您注意到“CHART”一词,然后有一行显示“CHART;”,则表示字符串的结尾。

使用此格式时要记住的重要一点是,无论您使用什么字符串来定义字符串结尾(例如本例中的 CHART),该单词都必须单独出现在一行上,后跟分号,并且 NO字符可以出现在同一行的分号之后,甚至是空格,否则 PHP 认为它是字符串的一部分。

It's a heredoc, for long strings that you don't have to worry about quotation marks and whatnot. If you notice the word CHART and then there's a line that says CHART;, that indicates the end of the string.

The important thing to remember when using this format is that whatever string you use to define the end of the string (such as CHART in this case), that word has to appear on a line on its own, followed by a semicolon, and NO characters can occur after the semicolon on the same line, even whitespace, otherwise PHP thinks it's part of the string.

末骤雨初歇 2024-09-25 11:56:17

这是 heredoc 语法

$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

It's the heredoc syntax.

$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文