5 PHP Useful Built-in Constants

PHP has a lot of built-in constants. Some are useful when working with files, and some with integers. But they all have one thing in common - they are all available globally. This short tutorial will look at typical examples and use cases found in Laravel.

1. PHP_VERSION - Quickly Check PHP Version

As the name implies, this will return the current PHP version. This is useful when checking if your application's current PHP version is compatible. This is found in Laravel HTTP tests:

public function testImageBmp()
$image = (new FileFactory)->image('test.bmp');
$imagePath = $image->getRealPath();
if (version_compare(PHP_VERSION, '8.3.0-dev', '>=')) {
$this->assertSame('image/bmp', mime_content_type($imagePath));
} else {
$this->assertSame('image/x-ms-bmp', mime_content_type($imagePath));

This constant returns a string like this: 8.3.0.

2. PHP_INT_MAX - Maximum Integer Value

If you ever needed to find the maximum integer value - you most likely typed something like 32412341241451 or searched for it online. But PHP has a constant for that. This is found in Laravel Lottery:

protected static function resultFactory()
return static::$resultFactory ?? fn ($chances, $outOf) => $outOf === null
? random_int(0, PHP_INT_MAX) / PHP_INT_MAX <= $chances
: random_int(1, $outOf) <= $chances;

This will return the maximum integer value - 9223372036854775807 on 64-bit systems. This is useful when generating a random number between 0 and the maximum integer value.

3. DIR - Current Directory

Our following constant is excellent if you need to get the current directory. This is found in Laravel Model Make Command:

protected function resolveStubPath($stub)
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: __DIR__.$stub;

This constant is helpful as it returns the full path to the current directory. For example, in my case:


From here, I can know my current directory and use it to find other files.

4. DIRECTORY_SEPARATOR - Directory Separator

This constant is useful when you need to get the directory separator. You can usually type / in the path, but sometimes it might not work (most likely in Windows), and you will get a 500 error. This is found in Laravel Lang Publish Command:

foreach ($stubs as $from => $to) {
if ((! $this->option('existing') && (! file_exists($to) || $this->option('force')))
|| ($this->option('existing') && file_exists($to))) {
file_put_contents($to, file_get_contents($from));

This prevents errors and will return system-specific separation characters. In Linux/osX, it will return /, and in Windows, \.

5. PHP_EOL - End Of Line

Last on our list is PHP_EOL. This constant helps you out when you need to add a new line in a file you generate. This automatically returns the correct end-of-line character for your system. This is found in Laravel Controller Make Command:

protected function buildFormRequestReplacements(array $replace, $modelClass)
// ...
if ($storeRequestClass !== $updateRequestClass) {
$namespacedRequests .= PHP_EOL.'use '.$namespace.'\\'.$updateRequestClass.';';
return array_merge($replace, [
// ...

This will return \n in Linux/osX and \r\n in Windows, preventing errors and ensuring your code is compatible with all systems.

More Constants?

We recommend you check out the PHP Documentation for more constants:

Or, if you are feeling adventurous, you can run this in your local environment:


This will print all constants that are available in your system.

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 56 courses (1011 lessons, total 46 h 42 min)
  • 77 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials