php syntax for new line is not working

拜拜、爱过 提交于 2019-12-24 12:16:46

问题


I am having a rather simple question

can anyone tell me why this is not displaying each variable on a new line, well except for the <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;

thanks Richard


回答1:


Carriage returns have no effect if you're viewing this output as HTML, so try turning them into <br> tags with nl2br...

echo nl2br($oremarks);



回答2:


Because you are outputting the result in the browser window, try "<br />" instead of "\n".




回答3:


Presumably because you are generating HTML source code from PHP, and not plain text.

In HTML, a new line is treated like any other whitespace. You need a <br> element or something that is display: block (or similar) to trigger a line break.




回答4:


Try alternating the Double quotes for your array values, use single quotes instead

$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;

But I would recommend using a HEREDOC instead of concatenating a string

    $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;



回答5:


In html, newlines never go to line. You have to put <br> in your source.

Notice that php can also works independently from http server as a command line utility and does not necessarily generate html.

Thus if you set your content type in web server to plain/text instead of html using header("Content-type: plain/text"); at the beginning of your file your text would go to line as you expected.




回答6:


\n only shows a newline in the source.
is the "newline" character for HTML.




回答7:


The '\n' is just creating a new line in the html code, it is not creating a new line that is visible. You need to use html to get the new line to be visible. You can use the the html break <br> or you can make each line a paragraph <p> your text... </p> or you can use a list:

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



回答8:


Try putting in the following:

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


来源:https://stackoverflow.com/questions/1492945/php-syntax-for-new-line-is-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!