# CLI Constant

- Thread: https://externals.io/message/13081
- Original archive: https://news-web.php.net/php.internals/13081
- Started by Jason Garber on 2004-09-30T21:10:01+00:00
- Messages: 10
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #13081 — Jason Garber — 2004-09-30T21:10:01+00:00

URL: https://externals.io/message/13081#13081

Hello internals,

  What is the best way, from within a php script, to tell that the
  script is being run on the command line?

  I've used   if(defined('STDIN'))  but that seems kind of kludgey.

  If there is no better way, perhaps we could add constant called
  (SAPI_TYPE = 'CLI') or something like that to the language to
  facilitate this?

-- 
Best regards,
 Jason Garber                      mailto:jason@ionzoft.com
 IonZoft, Inc.

---

## #13083 — Rasmus Lerdorf — 2004-09-30T21:37:40+00:00

URL: https://externals.io/message/13081#13083 · In reply to: #13081

php_sapi_name()



> Hello internals,
>
>   What is the best way, from within a php script, to tell that the
>   script is being run on the command line?
>
>   I've used   if(defined('STDIN'))  but that seems kind of kludgey.
>
>   If there is no better way, perhaps we could add constant called
>   (SAPI_TYPE = 'CLI') or something like that to the language to
>   facilitate this?
>
> --
> Best regards,
>  Jason Garber                      mailto:jason@ionzoft.com
>  IonZoft, Inc.

---

## #13084 — George Schlossnagle — 2004-09-30T21:39:33+00:00

URL: https://externals.io/message/13081#13084 · In reply to: #13081

> Hello internals,
>
>   What is the best way, from within a php script, to tell that the
>   script is being run on the command line?
>
>   I've used   if(defined('STDIN'))  but that seems kind of kludgey.
>
>   If there is no better way, perhaps we could add constant called
>   (SAPI_TYPE = 'CLI') or something like that to the language to
>   facilitate this?

You could use:

define(SAPI_TYPE, strtoupper(php_sapi_name()));

to get your constant.

George

---

## #13085 — Michael Sims — 2004-09-30T21:45:52+00:00

URL: https://externals.io/message/13081#13085 · In reply to: #13081

Jason Garber wrote:
> Hello internals,
> 
>   What is the best way, from within a php script, to tell that the
>   script is being run on the command line?
> 
>   I've used   if(defined('STDIN'))  but that seems kind of kludgey.

I've used

if (php_sapi_name() == 'cli') ...

which seems to work well.

---

## #13086 — Edin Kadribasic — 2004-09-30T21:50:14+00:00

URL: https://externals.io/message/13081#13086 · In reply to: #13081

Checkout the `PHP_SAPI` constant .

Edin

----- Original Message ----- 
From: "Jason Garber" <jason@ionzoft.com>
To: <internals@lists.php.net>
Sent: Thursday, September 30, 2004 11:10 PM
Subject: [PHP-DEV] CLI Constant


> Hello internals,
> 
>   What is the best way, from within a php script, to tell that the
>   script is being run on the command line?
> 
>   I've used   if(defined('STDIN'))  but that seems kind of kludgey.
> 
>   If there is no better way, perhaps we could add constant called
>   (SAPI_TYPE = 'CLI') or something like that to the language to
>   facilitate this?
> 
> -- 
> Best regards,
>  Jason Garber                      mailto:jason@ionzoft.com
>  IonZoft, Inc.

---

## #13087 — Andrey Hristov — 2004-09-30T21:50:25+00:00

URL: https://externals.io/message/13081#13087 · In reply to: #13081

Jason Garber wrote:
> Hello internals,
> 
>   What is the best way, from within a php script, to tell that the
>   script is being run on the command line?
> 
>   I've used   if(defined('STDIN'))  but that seems kind of kludgey.
> 
>   If there is no better way, perhaps we could add constant called
>   (SAPI_TYPE = 'CLI') or something like that to the language to
>   facilitate this?
> 

I think this is a good idea.
A hackish way is also
if (in_array($_SERVER['argv'][0], array($_SERVER['PHP_SELF'],'-'), 1))
//the second one is for php -r

Andrey

---

## #13088 — Marcus Boerger — 2004-09-30T21:50:34+00:00

URL: https://externals.io/message/13081#13088 · In reply to: #13081

Hello Jason,

  you lazy doc reader :-)

  php -r 'var_dump(php_sapi_name());'

best regards
marcus


Thursday, September 30, 2004, 11:10:01 PM, you wrote:

> Hello internals,

>   What is the best way, from within a php script, to tell that the
>   script is being run on the command line?

>   I've used   if(defined('STDIN'))  but that seems kind of kludgey.

>   If there is no better way, perhaps we could add constant called
>   (SAPI_TYPE = 'CLI') or something like that to the language to
>   facilitate this?

> -- 
> Best regards,
>  Jason Garber                      mailto:jason@ionzoft.com
>  IonZoft, Inc.




-- 
Best regards,
 Marcus                            mailto:helly@php.net

---

## #13090 — Jason Garber — 2004-09-30T21:54:27+00:00

URL: https://externals.io/message/13081#13090 · In reply to: #13088

Hello Marcus,

  Ok, Ok.  I admit it.

  I made the POINT of reading the CLI manual page before posting this,
  but I just missed it.

  Sorry for the trouble, and, thanks for the help.

-- 
Best regards,
 Jason                            mailto:jason@ionzoft.com

Thursday, September 30, 2004, 5:50:34 PM, you wrote:

MB> Hello Jason,

MB>   you lazy doc reader :-)

MB>   php -r 'var_dump(php_sapi_name());'

MB> best regards
MB> marcus


MB> Thursday, September 30, 2004, 11:10:01 PM, you wrote:

> > […]

> > […]

> > […]

> > […]

> > […]

---

## #13089 — Robert Silva — 2004-09-30T21:54:46+00:00

URL: https://externals.io/message/13081#13089 · In reply to: #13081

PHP_SAPI constant or php_sapi_name



-----Original Message-----
From: Jason Garber [mailto:jason@ionzoft.com] 
Sent: Thursday, September 30, 2004 2:10 PM
To: internals@lists.php.net
Subject: [PHP-DEV] CLI Constant

Hello internals,

  What is the best way, from within a php script, to tell that the
  script is being run on the command line?

  I've used   if(defined('STDIN'))  but that seems kind of kludgey.

  If there is no better way, perhaps we could add constant called
  (SAPI_TYPE = 'CLI') or something like that to the language to
  facilitate this?

-- 
Best regards,
 Jason Garber                      mailto:jason@ionzoft.com
 IonZoft, Inc.

---

## #13091 — Timm Friebe — 2004-09-30T21:59:08+00:00

URL: https://externals.io/message/13081#13091 · In reply to: #13081

> Hello internals,
[...]
>   If there is no better way, perhaps we could add constant called
>   (SAPI_TYPE = 'CLI') or something like that to the language to
>   facilitate this?

$ php -r 'var_dump(PHP_SAPI);'
string(3) "cli"

 `get_defined_constants()` is your friend:)

- Timm
