2

I've found this line in one of the codes coppied from web but have no ide why they use char() function at the end of it. Please let me know why it is used, what it does and what if I don't use it all.

Thanks in advance.

CODE:

$crlf = chr(13) . chr(10);

echo '<html><head><title>Whatever</title></head><body>Hello</body></html>' . $crlf;
BentCoder
  • 12,257
  • 22
  • 93
  • 165
  • 4
    It's instead of writing `"\r\n"`. – gen_Eric Jun 13 '13 at 14:35
  • to echo a line break. – michi Jun 13 '13 at 14:36
  • 1
    They are not using char at the end of it, they are using whatever the function returns, in this case it would be: the CarriageReturn (\r) and LineFeed(\n) in this case (a new line) – aleation Jun 13 '13 at 14:36
  • The variable name says it all :-) – HamZa Jun 13 '13 at 14:36
  • @TerrySeidler `PHP_EOL` is platform related, so if you had a website on windows and then ported it to linux you could encounter some problems later on. – HamZa Jun 13 '13 at 14:38
  • `PHP_EOL` *should* be platform-independent as far as I know: http://stackoverflow.com/questions/128560/when-do-i-use-the-php-constant-php-eol – Terry Seidler Jun 13 '13 at 14:39
  • Thanks guys I learned it now. – BentCoder Jun 13 '13 at 14:39
  • @TerrySeidler What I mean is the following, if you wrote `PHP_EOL` to a file, and later on you used `explode("\r\n", $data);` on windows it would work fine, but if you then go on linux, `PHP_EOL` will not use `\r\n`. – HamZa Jun 13 '13 at 14:41

4 Answers4

6

chr(13) is equivalent to "\r" and chr(10) is equivalent to "\n".

So, $crlf = chr(13) . chr(10); is a string containing "\r\n", a CRLF (newline) as the variable name states.

Jacob Krall
  • 28,341
  • 6
  • 66
  • 76
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
2

chr(13) and chr(10) return the ASCII representations of "Carriage return" and "Line feed". These characters can also be written as "\r\n" in PHP. On a Windows system, CR+LF together represent a newline. (See What is the difference between \r and \n?)

Community
  • 1
  • 1
Jacob Krall
  • 28,341
  • 6
  • 66
  • 76
0

The "chr()" function returns a specific ASCII value. See php chr function and ascii chart - Which should help you understand more :)

laminatefish
  • 5,197
  • 5
  • 38
  • 70
0

The questioner asked

Please let me know why it is used, what it does and what if I don't use it all.

And I don't think anybody answered that yet ...

The programmer is presumably trying to format the HTML code of the page so that is is more readable when displayed through a browser's 'view source' functionality. So the $crlf could be omitted safely, so long as you don't care what the HTML source looks like.

Seems an odd decision to use two function calls rather than typing "\r\n", but who knows, the coder may have had his/her reasons.

fred2
  • 1,015
  • 2
  • 9
  • 29