[prev in list] [next in list] [prev in thread] [next in thread]
List: php-doc-cvs
Subject: [DOC-CVS] [doc-en] master: [PHP 8.1] Passing null to non-nullable parameters of built-in functions (
From: Sergey Panteleev via GitHub <noreply () php ! net>
Date: 2022-04-28 9:26:32
Message-ID: VUmNdSen1ZOlrijEQuPhQH82ZfUNQQeAd0Wur5W7bWw () main ! php ! net
[Download RAW message or body]
Author: Sergey Panteleev (saundefined)
Committer: GitHub (web-flow)
Pusher: saundefined
Date: 2022-04-28T12:22:42+03:00
Commit: https://github.com/php/doc-en/commit/3e087a5c618b815c273510ffdc4a86ab189eda00
Raw diff: https://github.com/php/doc-en/commit/3e087a5c618b815c273510ffdc4a86ab189eda00.diff
[PHP 8.1] Passing null to non-nullable parameters of built-in functions (#1555)
Closes #1554
Co-authored-by: Máté Kocsis <kocsismate@woohoolabs.com>
Co-authored-by: Christoph M. Becker <cmbecker69@gmx.de>
Changed paths:
M language/functions.xml
Diff:
diff --git a/language/functions.xml b/language/functions.xml
index 248c48d2eb9..d59c2ab0deb 100644
--- a/language/functions.xml
+++ b/language/functions.xml
@@ -1064,6 +1064,38 @@ $func(); // prints "bar"
be thrown in this case.
</simpara>
</note>
+ <note>
+ <para>
+ Scalar types for built-in functions are nullable by default in coercive mode.
+ As of PHP 8.1.0, passing &null; to an internal function parameter that is not \
declared nullable + is discouraged and emits a deprecation notice in coercive \
mode to align with the behavior of user-defined functions, + where scalar types \
need to be marked as nullable explicitly. + </para>
+
+ <para>
+ For example, <function>strlen</function> function expects the parameter \
<literal>$string</literal> + to be a non-nullable &string;.
+ For historical reasons, PHP allows passing &null; for this parameter in \
coercive mode, and the parameter is + implicitly cast to <type>string</type>, \
resulting in a <literal>""</literal> value. + In contrast, a \
<classname>TypeError</classname> is emitted in strict mode. + </para>
+
+ <example>
+ <programlisting role="php">
+<![CDATA[
+<?php
+var_dump(strlen(null));
+// "Deprecated: Passing null to parameter #1 ($string) of type string is deprecated" \
as of PHP 8.1.0 +// int(0)
+
+var_dump(str_contains("foobar", null));
+// "Deprecated: Passing null to parameter #2 ($needle) of type string is deprecated" \
as of PHP 8.1.0 +// bool(true)
+?>
+]]>
+ </programlisting>
+ </example>
+ </note>
<sect2 role="seealso">
&reftitle.seealso;
--
PHP Documentation Commits Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic