\ r \ n和\ n之间的PHP区别 [英] PHP difference between \r\n and \n

查看:93
本文介绍了\ r \ n和\ n之间的PHP区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题...

Simple question...

我已经看到有人告诉我在不同的地方使用"\ r \ n",而其他人则告诉我在同一位置使用"\ n".我确定一个人是对的,一个人是错误的.示例-设计mail()标头时:

I have seen people tell me to use "\r\n" in various places and others tell me to use "\n" in the same place. I'm sure one is right and one is wrong. Example - when designing mail() headers:

教程1:

//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 

教程2(请注意标头参数):

Tutorial #2 (notice the header argument):

mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "bcc: " . $bcc . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/alternative;\n" . 
    "     boundary=" . $mime_boundary_header)

我很困惑,但是很明显,这有所不同,因为使用一个标题,我的标头起作用了,而使用另一个标头,有时起作用.

I am confused, but clearly it makes somewhat of a difference, because with one, my headers worked, and with the other they only sometimes work.

推荐答案

\ r \ n是Windows系统的行尾字符.

\r\n are end of line characters for Windows systems.

\ n是UNIX系统的行尾字符.

\n is the end of line character for UNIX systems.

这些字符是不可见的.根据我自己的经验,\ n通常也适用于Windows.

These characters are invisible. From my own experience \n is usually okay for Windows as well.

有些人更喜欢使用 PHP_EOL 常量代替这些常量平台之间可移植性的字符.

Some prefer to use PHP_EOL constant instead of these characters for portability between platforms.

echo 'hi' . PHP_EOL;
echo "hi\n";

$headers = "From: webmaster@example.com" . PHP_EOL 
           . "Reply-To: webmaster@example.com"; 

这篇关于\ r \ n和\ n之间的PHP区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆