Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

Cross-Platform Code 3: Path and Line Separators

Each OS has a different way of representing path and line separators for files. Unix and modern Mac OS versions use / as a path separator and \n as a line separator, whereas Windows uses \ or / as a path separator and \r\n as a line separator. Just to make things even more confusing, some old Mac OS versions use \r as a line separator and : as a path separator, so all three are different!

You can make your life easier by using forward slashes (/) everywhere, because Windows accepts both \ and / as path separators. If you are able to refrain from using OS-specific path names like c:/home/website/index.php, then do—very often, just /home/website/index.php will work just fine everywhere.

Line separators are slightly trickier and, if you don't have PHP 5.0.2 or higher, the easiest way to handle them is to put a few lines of code into your shared code library that checks the OS and stores the appropriate line end character in a variable—you can then reuse that variable throughout your other scripts. If you do have PHP 5.0.2 or higher, the constant PHP_EOL is available to you and represents the appropriate newline character for the current OS.

Warning

Using the OS-specific newline character, e.g., \r\n on Windows, is not a smart move if you want the generated files to be portable to other platforms. This is because a script running on Windows will load and save files with \r\n as line ends, whereas the same script on Unix will use just \n. So, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page