Both sides previous revisionPrevious revisionNext revision | Previous revision |
rfc:strict_operators:faq [2020/07/06 14:56] – switch is not affected jasny | rfc:strict_operators:faq [2020/07/06 15:01] (current) – jasny |
---|
Both the concatenation operator and arithmetic operators throw a ''%%TypeError%%'' for arrays, resources, and objects. Casting these to a string or int/float doesn't give a proper representation of the value of the operand. | Both the concatenation operator and arithmetic operators throw a ''%%TypeError%%'' for arrays, resources, and objects. Casting these to a string or int/float doesn't give a proper representation of the value of the operand. |
| |
Using a boolean or null as operand for both concatenation and arithmetic operators also throws a ''%%TypeError%%''. In most cases, the use of a boolean or null indicates an error as many functions return ''%%false%%'' or ''%%null%%'' in case of an error or when no result can be returned. This is different from the function returning an empty string or ''%%0%%''. [[https://php.net/strpos|''%%strpos%%'']] is a well-known example. | Using a boolean or null as operand for both concatenation and arithmetic operators also throws a ''%%TypeError%%''. In most cases, the use of a boolean or null indicates an error as many functions return ''%%false%%'' or ''%%null%%'' in case of an error or when no result can be returned. This is different from the function returning an empty string or ''%%0%%''. [[https://php.net/strpos|strpos]] is a well-known example. |
| |
==== Will comparing a number to a numeric string work with strict_operators? ==== | ==== Will comparing a number to a numeric string work with strict_operators? ==== |
==== Why is switch not affected? ==== | ==== Why is switch not affected? ==== |
| |
Using strict_operators will apply only result in cases where a ''TypeError'' is thrown. It will not affect the behavior, giving a different result. | Using strict_operators will only result in cases where a ''TypeError'' is thrown. It will not affect the behavior, giving a different result. |
| |
Changing ''switch'' to compare using ''==='' would change the behavior based on the directive. | Changing ''switch'' to compare using ''==='' would change the behavior based on the directive. |
| |
To use strict comparison, most ''switch'' statements can be rewritten to [[https://wiki.php.net/rfc/match_expression_v2|''match'' statements]. | To use strict comparison, most ''switch'' statements can be rewritten to [[https://wiki.php.net/rfc/match_expression_v2|match statements]]. |
| |
==== Are there cases where a statement doesn't throw a TypeError but yields a different result? ==== | ==== Are there cases where a statement doesn't throw a TypeError but yields a different result? ==== |