Commit 40514c7
Clarify in_array() strict comparison behavior for PHP 8.0+ (#5519)
* docs: clarify in_array() strict comparison for modern PHP
Update the note for the strict parameter to accurately reflect PHP 8.0.0
changes and highlight ongoing loose comparison risks with booleans.
* in_array: fix the strict note and restore the unconditional advice
The note said a true value in the haystack matches any non-empty string
needle. Comparing a bool with a string casts the string to bool, and the
two falsy strings are "" and "0", so in_array("0", [true]) is false.
Say truthy instead, and add the false side of the same trap, which the
removed "Similar edge cases exist for other types" sentence used to hint
at.
"As of PHP 8.0.0, this behavior is restricted to numeric strings" reads
as if a numeric string still matched 0 in general. State the mechanism
the way the manual already does on the PHP 8.0 backward incompatible
changes page: the number is converted to a string and the values are
compared as strings.
Restore "always" on the recommendation. The previous wording, "it is
recommended to", was weaker than the text it replaced, and strict does
not give type safety, it selects an identity comparison.
Link the loose comparison table rather than paraphrasing it, since
paraphrasing it from memory is what produced the non-empty claim.
Markup: &true; already expands to a constant element, so wrapping it in
another one nested constant inside constant, which DocBook rejects and
which is why the build was failing. Use simpara for the inline-only
paragraphs, type instead of literal for the string type name, and
restore the closing tag indentation.
Sources
- Zend/zend_operators.c, the default branch of zend_compare(), and
Zend/zend_operators.h, the IS_STRING case of i_zend_is_true()
- Zend/zend_operators.c, compare_long_to_string()
- appendices/migration80/incompatible.xml, string to number comparison
- doc-base/entities/global.ent, the true and false entities
- Verified on PHP 8.5.4: in_array("0", [true]) is false,
in_array("0", [false]) is true, in_array("foo", [0]) is false
---------
Co-authored-by: Louis-Arnaud <la.catoire@gmail.com>1 parent 3c1b114 commit 40514c7
1 file changed
Lines changed: 17 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
63 | 74 | | |
64 | 75 | | |
65 | 76 | | |
| |||
0 commit comments