Oihana PHP

env

Table of Contents

Functions

completePath()  : string
Completes a PATH with the standard directories it lacks, added at its end.
cpuCount()  : int
Returns the number of CPUs available (if detectable).
isCli()  : bool
Checks if the PHP script is running in CLI mode (terminal, cron, Symfony Console…).
isCliWithFile()  : bool
Checks if the PHP script is running in CLI mode **and** was launched with an actual file.
isColorTerminal()  : bool
Checks if the CLI supports colors.
isCron()  : bool
Checks if the script is likely running from a cron job.
isDebug()  : bool
Checks if PHP is running in debug mode.
isDocker()  : bool
Checks if PHP is running inside a Docker container.
isExtensionLoaded()  : bool
Checks if a PHP extension is loaded.
isInteractive()  : bool
Checks if the script is running in an interactive CLI (php -a or terminal input).
isLinux()  : bool
Indicates if the OS system is Linux.
isMac()  : bool
Indicates if the OS system is Mac.
isOtherOS()  : bool
Checks if the operating system is **not Windows, Mac, or Linux**.
isWeb()  : bool
Checks if the PHP script is running in a Web server context (not CLI).
isWindows()  : bool
Indicates if the OS system is Windows.
phpVersion()  : string
Returns the current PHP version.
programLocations()  : array<string, string|null>
Finds where a PATH finds each program, as a shell would.

Functions

completePath()

Completes a PATH with the standard directories it lacks, added at its end.

completePath(string $path[, string $standard = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' ]) : string

A program started by cron, by a systemd unit or by any other way than a login shell does not receive the PATH of a session. Cron, on many systems, gives /usr/bin:/bin and nothing more: a program installed in /usr/local/bin is found by hand and missing at night, and what fails is whatever needed it, often far from the cause. Some systems mend it — Ubuntu's cron reads /etc/environment — and others do not, so a program that depends on its PATH should not trust the one it is given.

Nothing is removed or moved: the standard directories come after those already there, so a program an administrator put earlier in the PATH still wins. Empty segments are dropped, and a directory is never listed twice by this function.

Parameters
$path : string

The PATH as received; empty when there is none.

$standard : string = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'

The directories a PATH should hold, in their order, as a PATH. By default the one Debian and Ubuntu give a login session.

Tags
example
use function oihana\core\env\completePath;

completePath( '/usr/bin:/bin' ) ;
// '/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/sbin'

putenv( 'PATH=' . completePath( (string) getenv( 'PATH' ) ) ) ;
author

Marc Alcaraz (ekameleon)

since
1.4.0
Return values
string —

The PATH, completed.

cpuCount()

Returns the number of CPUs available (if detectable).

cpuCount() : int
Tags
author

Marc Alcaraz

since
1.0.0
Return values
int —

The number of CPUs available (if detectable), null otherwise.

isCli()

Checks if the PHP script is running in CLI mode (terminal, cron, Symfony Console…).

isCli() : bool

Returns true if PHP is executed from the command line.

Tags
example
use function oihana\core\env\isCli;

if (isCli())
{
    echo "CLI mode\n";
}
author

Marc Alcaraz

since
1.0.0
Return values
bool —

true if the script is running in CLI, false otherwise

isCliWithFile()

Checks if the PHP script is running in CLI mode **and** was launched with an actual file.

isCliWithFile() : bool

Returns true if PHP is executed from the command line and $_SERVER['argv'][0] points to an existing file.

Tags
example
use function oihana\core\env\isCliWithFile;

if (isCliWithFile())
{
    echo "CLI mode with script file\n";
}
else
{
    echo "Not a CLI script with a file\n";
}
author

Marc Alcaraz

since
1.0.0
Return values
bool —

true if running in CLI with a real script file, false otherwise

isColorTerminal()

Checks if the CLI supports colors.

isColorTerminal() : bool
Tags
author

Marc Alcaraz

since
1.0.0
Return values
bool —

true if the CLI support colors, false otherwise

isCron()

Checks if the script is likely running from a cron job.

isCron() : bool
Tags
author

Marc Alcaraz

since
1.0.0
Return values
bool —

true if likely a cron job, false otherwise

isDebug()

Checks if PHP is running in debug mode.

isDebug() : bool
Tags
author

Marc Alcaraz

since
1.0.0
Return values
bool —

true if PHP is running in debug mode, false otherwise

isDocker()

Checks if PHP is running inside a Docker container.

isDocker() : bool
Tags
author

Marc Alcaraz

since
1.0.0
Return values
bool —

true if running in Docker, false otherwise

isExtensionLoaded()

Checks if a PHP extension is loaded.

isExtensionLoaded(string $ext) : bool
Parameters
$ext : string

The case-insensitive name of the extension (e.g. 'intl', 'bcmath').

Tags
example
use function oihana\core\env\isExtensionLoaded;

if ( isExtensionLoaded( 'intl' ) )
{
    // safe to use grapheme_* / Normalizer
}
author

Marc Alcaraz

since
1.0.0
Return values
bool —

true if a PHP extension is loaded, false otherwise

isInteractive()

Checks if the script is running in an interactive CLI (php -a or terminal input).

isInteractive() : bool
Tags
example
use function oihana\core\env\isInteractive;

if ( isInteractive() )
{
   echo "Interactive cli mode\n";
}
author

Marc Alcaraz

since
1.0.0
Return values
bool —

true if interactive CLI, false otherwise

isLinux()

Indicates if the OS system is Linux.

isLinux() : bool
Tags
example
use function oihana\core\env\isLinux;

if ( isLinux() )
{
    echo "Linux environment\n";
}
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
bool —

true if running on Linux, false otherwise

isMac()

Indicates if the OS system is Mac.

isMac() : bool
Tags
example
use function oihana\core\env\isMac;

if ( isMac() )
{
    echo "Mac environment\n";
}
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
bool —

true if running on Mac, false otherwise

isOtherOS()

Checks if the operating system is **not Windows, Mac, or Linux**.

isOtherOS() : bool
Tags
author

Marc Alcaraz

since
1.0.0
Return values
bool —

true if the OS is neither Windows, Mac, nor Linux; false otherwise

isWeb()

Checks if the PHP script is running in a Web server context (not CLI).

isWeb() : bool

Returns true if PHP is executed via HTTP (e.g., Slim, Symfony HTTP, WordPress).

Tags
example
use function oihana\core\env\isWeb;

if (isWeb())
{
   echo "Web mode\n";
}
author

Marc Alcaraz

since
1.0.0
Return values
bool —

true if the script is running in a web context, false otherwise

isWindows()

Indicates if the OS system is Windows.

isWindows() : bool
Tags
example
use function oihana\core\env\isWindows;

if ( isWindows() )
{
    echo "Windows environment\n";
}
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
bool —

true if running on Windows, false otherwise

phpVersion()

Returns the current PHP version.

phpVersion() : string
Tags
example
use function oihana\core\env\phpVersion;

echo phpVersion() ;
author

Marc Alcaraz

since
1.0.0
Return values
string —

The current PHP version.

programLocations()

Finds where a PATH finds each program, as a shell would.

programLocations(array<string|int, string> $programs, string $path) : array<string, string|null>

For each name, the first file of that name in the directories of the PATH, in their order. A file that is there but not executable is not found: the shell would not run it.

Asked of a PATH other than the current one, it tells what a program started elsewhere — by cron, by a service — will find, which is not what a session finds. Paired with completePath(), it says whether completing a PATH is enough.

Parameters
$programs : array<string|int, string>

The program names, as a command line spells them.

$path : string

The PATH to look in.

Tags
example
use function oihana\core\env\programLocations;

programLocations( [ 'tar' , 'nothing-by-that-name' ] , '/usr/bin:/bin' ) ;
// [ 'tar' => '/usr/bin/tar' , 'nothing-by-that-name' => null ]
author

Marc Alcaraz (ekameleon)

since
1.4.0
Return values
array<string, string|null> —

Each program, in the order given, with the file the PATH finds for it, or null.

On this page

Search results