在PHP中,有什么作用。代表?

发布于 2025-01-24 06:19:35 字数 67 浏览 0 评论 0原文

例如:

$sql = <<<MySQL_QUERY

For example:

$sql = <<<MySQL_QUERY

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

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

发布评论

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

评论(8

累赘 2025-01-31 06:19:35

那是Heredoc语法。您可以通过将&lt;&lt;&lt;&lt;&lt;加上您选择的代币启动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;
我喜欢麦丽素 2025-01-31 06:19:35

这称为a heredoc ,它可以让您进行一段长的文本,这些文本会遍布几行。您可以将PHP变量放入其中,它们将替换为值。一词图可以是任何东西。仅需开始并停止引用文本开始的地方,就只需要相同。

您可以通过附加多个引用的字符串来完成相同的操作,但这在大多数时候对于此HTML文本(例如HTML文本)来说更为清洁。还有一种叫做a 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.

奶茶白久 2025-01-31 06:19:35

它是使用 Heredoc语法。

第三种定界字符串的方法是Heredoc语法:&lt;&lt;。

在此操作员后,提供了标识符,然后提供了一个newline。字符串本身遵循,然后再次使用相同的标识符来关闭报价。

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.

云裳 2025-01-31 06:19:35

是php的

例子:

$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;           
玩心态 2025-01-31 06:19:35

这是Heredoc,对于长字符串,您不必担心引用标记等。如果您注意到图表一词,然后有一行说图表;这表示字符串的末尾。

使用这种格式时要记住的重要事情是,无论您用来定义字符串末端的字符串(例如在这种情况下)角色可以在同一条线上的半分钟之后发生,甚至是空格,否则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.

情感失落者 2025-01-31 06:19:35

这是

$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;
难理解 2025-01-31 06:19:35

我发现Heredocnowdocphp中实用了极限且有用,我很惊讶,到目前为止,没有人给您提供更多的示例做。

首先,heredocnowdoc很简单,

  • heredoc:就像“双引号”字符串,您可以放置​​变量
  • nowdoc:就像''单Quote string 没有变量被解析

在以下示例中,我只会显示heredoc,要制作nowdoc只需将令牌包装在单引号中 - &gt; 'token'。

功能和优势

  • 可以根据需要添加“”和“”,并且不会引起任何
  • 带有动态变量的HTML代码,避免使用useell串联。
  • 将其存储在变量中以供字母使用,可以创建小组件并将其输出。
  • 这些线条实际上是用“ \ 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添加&lt; br&gt;每行

这起作用,因为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. 创建小组件

     &lt;?php
             foreach($ tasks作为$ task){
                 //创建一个类似于组件的HTML
                 $ component =&lt;&lt;&lt; heredoc
                 &lt; div class =“ pure-u-1-3”&gt;
                     &lt; div class =“ card”&gt;
                         &lt; div class =“ card-header”&gt;
                            {$ task ['name']}
                         &lt;/div&gt;
                         &lt; div class =“ card-body”&gt;
                             &lt; h5 class =“ card-title”&gt; {$ task ['state']}&lt;/h5&gt;
                             &lt; p class =“ card-text”&gt; {$ task ['description']}&lt;/p&gt;
                             &lt; a href =“ view?model = todo_list&amp; task_id = {$ task ['id'']}“ class =” btn btn btn-primary“&gt;请参阅任务todos&lt;/a&gt;
                         &lt;/div&gt;
                     &lt;/div&gt;                    
                 &lt;/div&gt;
                 埃尔多克;
                 Echo $组件; // 输出
    
             }
    
         ?&gt;
     

或仅放入一个字符串,然后使用1个回声

    <?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

时光匆匆的小流年 2025-01-31 06:19:35

要了解一个明确的想法:

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

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

如果我们使用&lt;&lt;&lt;

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

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

结论:如果我们使用第一个方法,则必须将其转换为json_encode()以某种方式将其转换为需要一些处理。相反,我们可以使用&lt;&lt;&lt;操作员节省一些时间并获得一些干净的代码。 :)

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. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文