Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warnings to use php_sapi_name() to test when running as CLI #4337

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading