Skip to content

Commit

Permalink
Add warnings to use php_sapi_name() to test when running as CLI (#4337
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jimwins authored Dec 30, 2024
1 parent a442910 commit 74ba8fe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
21 changes: 21 additions & 0 deletions features/commandline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,27 @@
also be found in the <varname>$_SERVER</varname> array, for example:
<varname>$_SERVER['argv']</varname>.
</para>
<warning>
<para>
The presence of <varname>$argv</varname> or <varname>$_SERVER['argv']</varname>
is not a reliable indication that a script is being run from the
command line because they may be set in other contexts when
<link linkend="ini.register-argc-argv">register_argc_argv</link> is enabled.
The value returned by <function>php_sapi_name</function> should be checked
instead.
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
if (php_sapi_name() === 'cli') {
echo "This is being run from the command line!\n";
}
]]>
</programlisting>
</informalexample>
</para>
</warning>
</entry>
</row>
<row>
Expand Down
8 changes: 8 additions & 0 deletions language/predefined/variables/argv.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
is disabled.
</simpara>
</note>
<warning>
<simpara>
To test if a script is being run from the command
line, <function>php_sapi_name</function> should be used
instead of checking whether <varname>$argv</varname> or
<varname>$_SERVER['argv']</varname> is set.
</simpara>
</warning>
</refsect1>

<refsect1 role="examples">
Expand Down

0 comments on commit 74ba8fe

Please sign in to comment.