PHP Constants, the lesser known ones

Often, when looking into people’s code, I find little code fragments that could be designed a bit more elegant. Sometimes the code needs refactoring or sometimes some design patterns needs to be applied.
This time though, I’d like to look at some PHP constants that are lesser known, but can make your code cleaner, less error-prone and more portable.


Returns a line ending (EOL = End Of Line), depending on the operating system you run your code on.

$greeting = 'hello world';
$name = 'felix';
echo $greeting . PHP_EOL . $name;

hello world


Returns the directory separator for the current operating system. On Linux this is ‘/’, but for example on a mac, this is ‘:’. Using DIRECTORY_SEPARATOR increases portability of your code.

$path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'site.class.php';


Returns the upper bound value an integer can hold for the current operating system. Values will be different on 32-bit or 64-bit systems, so when dealing with large numbers on different hardware systems, take this in consideration.


Returns the operating system that PHP is installed on.

if (PHP_OS == 'Linux') {
echo 'yay, linux!';
} else {
echo 'ugh, windows..';


Returns the SAPI that you’re currently using. This is the same as the PHP function php_sapi_name(), but supposedly should work a bit faster, because there is no need for a function call. PHP_SAPI can be interesting if you want to write code that runs on either a website or independently on PHP Commandline (in which case, PHP_SAPI outputs CLI or CGI).

// Check if we're running commandline,
// in which case we can thread our application
if (substr(PHP_SAPI, 0, 3) == 'cli') {
$pid = pcntl_fork();
// ...