【问题标题】:php syntax for new line is not working新行的php语法不起作用
【发布时间】:2009-09-29 14:34:15
【问题描述】:

我有一个相当简单的问题

谁能告诉我为什么这不是在新行上显示每个变量,除了 <br>

$curtime = gmdate("d/m/Y H:i:s");
    //capture the PayPal returned information as order remarks
$oremarks =
"##$curtime##<br>".
"PayPal Transaction Information...\n".
"Txn Id: ".$ppInfo["txn_id"]."\n".
"Txn Type: ".$ppInfo["txn_type"]."\n".
"Item Number: ".$ppInfo["item_number"]."\n".
"Payment Date: ".$ppInfo["payment_date"]."\n".
"Payment Type: ".$ppInfo["payment_type"]."\n".
"Payment Status: ".$ppInfo["payment_status"]."\n".
"Currency: ".$ppInfo["mc_currency"]."\n".
"Payment Gross: ".$ppInfo["payment_gross"]."\n".
"Payment Fee: ".$ppInfo["payment_fee"]."\n".
"Payer Email: ".$ppInfo["payer_email"]."\n".
"Payer Id: ".$ppInfo["payer_id"]."\n".
"Payer Name: ".$ppInfo["first_name"]." ".$ppInfo["last_name"]."\n".
"Payer Status: ".$ppInfo["payer_status"]."\n".
"Country: ".$ppInfo["residence_country"]."\n".
"Business: ".$ppInfo["business"]."\n".
"Receiver Email: ".$ppInfo["receiver_email"]."\n".
"Receiver Id: ".$ppInfo["receiver_id"]."\n";

//Update database using $orderno, set status to Paid
//Send confirmation email to buyer and notification email to merchant
//Redirect to thankyou page
echo $oremarks;

谢谢理查德

【问题讨论】:

    标签: php syntax newline


    【解决方案1】:

    Carriage returns have no effect if you're viewing this output as HTML,所以试着用nl2br把它们变成&lt;br&gt;标签...

    echo nl2br($oremarks);
    

    【讨论】:

      【解决方案2】:

      由于您是在浏览器窗口中输出结果,请尝试使用"&lt;br /&gt;" 而不是“\n”。

      【讨论】:

        【解决方案3】:

        大概是因为您是从 PHP 生成 HTML 源代码,而不是纯文本。

        在 HTML 中,新行的处理方式与任何其他空格一样。您需要 &lt;br&gt; 元素或 display: block(或类似)元素来触发换行符。

        【讨论】:

          【解决方案4】:

          尝试为您的数组值交替使用双引号,改用单引号

          $curtime = gmdate("d/m/Y H:i:s");
              //capture the PayPal returned information as order remarks
          $oremarks =
          "##$curtime##<br>".
          "PayPal Transaction Information...\n".
          "Txn Id: ".$ppInfo['txn_id']."\n".
          "Txn Type: ".$ppInfo['txn_type']."\n".
          "Item Number: ".$ppInfo['item_number']."\n".
          "Payment Date: ".$ppInfo['payment_date']."\n".
          "Payment Type: ".$ppInfo['payment_type']."\n".
          "Payment Status: ".$ppInfo['payment_status']."\n".
          "Currency: ".$ppInfo['mc_currency']."\n".
          "Payment Gross: ".$ppInfo['payment_gross']."\n".
          "Payment Fee: ".$ppInfo['payment_fee']."\n".
          "Payer Email: ".$ppInfo['payer_email']."\n".
          "Payer Id: ".$ppInfo['payer_id']."\n".
          "Payer Name: ".$ppInfo['first_name']." ".$ppInfo['last_name']."\n".
          "Payer Status: ".$ppInfo['payer_status']."\n".
          "Country: ".$ppInfo['residence_country']."\n".
          "Business: ".$ppInfo['business']."\n".
          "Receiver Email: ".$ppInfo['receiver_email']."\n".
          "Receiver Id: ".$ppInfo['receiver_id']."\n";
          
          //Update database using $orderno, set status to Paid
          //Send confirmation email to buyer and notification email to merchant
          //Redirect to thankyou page
          echo $oremarks;
          

          但我建议使用 HEREDOC 而不是连接字符串

              $curtime = gmdate("d/m/Y H:i:s");
                  //capture the PayPal returned information as order remarks
              $oremarks =<<<OREMARKS
          ##$curtime##
          PayPal Transaction Information...
          Txn Id: $ppInfo['txn_id']
          Txn Type: $ppInfo['txn_type']
          Item Number: $ppInfo['item_number']
          Payment Date: $ppInfo['payment_date']
          Payment Type: $ppInfo['payment_type']
          Payment Status: $ppInfo['payment_status']
          Currency: $ppInfo['mc_currency']
          Payment Gross: $ppInfo['payment_gross']
          Payment Fee: $ppInfo['payment_fee']
          Payer Email: $ppInfo['payer_email']
          Payer Id: $ppInfo['payer_id']
          Payer Name: $ppInfo['first_name'] $ppInfo['last_name']
          Payer Status: $ppInfo['payer_status']
          Country: $ppInfo['residence_country']
          Business: $ppInfo['business']
          Receiver Email: $ppInfo['receiver_email']
          Receiver Id: $ppInfo['receiver_id']
          OREMARKS;
          
              //Update database using $orderno, set status to Paid
              //Send confirmation email to buyer and notification email to merchant
              //Redirect to thankyou page
              echo $oremarks;
          

          【讨论】:

          • 感谢heredoc语法,将来会派上用场
          【解决方案5】:

          在 html 中,换行从不换行。您必须将&lt;br&gt; 放入您的源代码中。

          请注意,php 也可以作为命令行实用程序独立于 http 服务器工作,并且不一定生成 html。

          因此,如果您在文件开头使用 header("Content-type: plain/text"); 将 Web 服务器中的内容类型设置为纯文本/文本而不是 html,您的文本将按预期排列。

          【讨论】:

            【解决方案6】:

            \n 仅在源代码中显示换行符。
            是 HTML 的“换行符”。

            【讨论】:

              【解决方案7】:

              '\n' 只是在 html 代码中创建一个新行,而不是创建一个可见的新行。您需要使用 html 使新行可见。您可以使用 html 分隔符 &lt;br&gt; 或者您可以将每一行设为一个段落 &lt;p&gt; your text... &lt;/p&gt; 或者您可以使用列表:

               <ul>
                 <li> your text... </li>
                 <li> next item... </li>
                 <li> more stuff.. </li>
               </ul>
              

              【讨论】:

                【解决方案8】:

                尝试输入以下内容:

                <?php
                echo "<pre>";
                .
                .
                .
                ?>
                

                【讨论】:

                  猜你喜欢
                  • 2020-04-01
                  • 2017-09-27
                  • 1970-01-01
                  • 2015-06-24
                  • 2017-09-20
                  • 2012-09-06
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多