Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PHP have a function to detect the OS it's running on?

I wouldn't know under what keyword to look for this in the PHP database, so I'm asking here.

Reason I want to know is because of how different Operating Systems handle new lines in textdocuments.

I'm using a CSV file in windows but each time I think I add a new line, what really happens is the new line gets pasted to the back of the latest line.

Reason is, in windows, a new line is this: \r\n And the CSVHandler.class.php file I'm using only adds \n

However, in MAC OS X that's the new line, which is different from windows.

So I'm looking for this so I can implement a simple if() statement and solve this. Currently I've hardcoded the \r\n, but it should be simpler, no?

like image 826
Vordreller Avatar asked Mar 08 '09 16:03

Vordreller


2 Answers

PHP has included the constant PHP_EOL for solving the problem you face, available since php 4.3.10 and PHP 5.0.2 - it contains a suitable end-of-line sequence for the server that PHP is running on.

If you want to use a different end-of-line sequence suitable for a particular client, then you'll have to code that yourself. One way to determine the client OS is to use get_browser, assuming your server has an up-to-date browscap.ini

like image 170
Paul Dixon Avatar answered Oct 29 '22 01:10

Paul Dixon


You could use the predefined constant PHP_OS.

I'm using

if (PHP_OS === 'WINNT') {...}

like image 24
mapsi Avatar answered Oct 28 '22 23:10

mapsi