Skip to content

Commit 74ba8fe

Browse files
authored
Add warnings to use php_sapi_name() to test when running as CLI (#4337)
1 parent a442910 commit 74ba8fe

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

features/commandline.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,27 @@
182182
also be found in the <varname>$_SERVER</varname> array, for example:
183183
<varname>$_SERVER['argv']</varname>.
184184
</para>
185+
<warning>
186+
<para>
187+
The presence of <varname>$argv</varname> or <varname>$_SERVER['argv']</varname>
188+
is not a reliable indication that a script is being run from the
189+
command line because they may be set in other contexts when
190+
<link linkend="ini.register-argc-argv">register_argc_argv</link> is enabled.
191+
The value returned by <function>php_sapi_name</function> should be checked
192+
instead.
193+
<informalexample>
194+
<programlisting role="php">
195+
<![CDATA[
196+
<?php
197+
198+
if (php_sapi_name() === 'cli') {
199+
echo "This is being run from the command line!\n";
200+
}
201+
]]>
202+
</programlisting>
203+
</informalexample>
204+
</para>
205+
</warning>
185206
</entry>
186207
</row>
187208
<row>

language/predefined/variables/argv.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
is disabled.
2626
</simpara>
2727
</note>
28+
<warning>
29+
<simpara>
30+
To test if a script is being run from the command
31+
line, <function>php_sapi_name</function> should be used
32+
instead of checking whether <varname>$argv</varname> or
33+
<varname>$_SERVER['argv']</varname> is set.
34+
</simpara>
35+
</warning>
2836
</refsect1>
2937

3038
<refsect1 role="examples">

0 commit comments

Comments
 (0)