rfc:strict_operators:faq

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:strict_operators:faq [2020/07/06 13:46] jasnyrfc:strict_operators:faq [2020/07/06 15:01] (current) jasny
Line 8: Line 8:
   * Variable parsing in strings specified in double quotes and with heredoc, is also affected.   * Variable parsing in strings specified in double quotes and with heredoc, is also affected.
   * Concatenation operation (using ''%%.%%'') on ''%%null%%'' does not throw a ''%%TypeError%%'', but will cast ''%%null%%'' to an empty string.   * Concatenation operation (using ''%%.%%'') on ''%%null%%'' does not throw a ''%%TypeError%%'', but will cast ''%%null%%'' to an empty string.
 +  * ''switch'' is not affected by this directive.
  
 ==== Why use a directive instead of applying this behavior as default? ==== ==== Why use a directive instead of applying this behavior as default? ====
Line 45: Line 46:
 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? ====
Line 98: Line 99:
 Throwing a ''%%TypeError%%'' only if the keys of the arrays don't match is not in line with this RFC. The behavior of an operator should not depend on the value of the operand, only on the type. Furthermore, a ''%%TypeError%%'' would be misplaced here, as some arrays would be accepted but others not, whereas a ''%%TypeError%%'' indicates no values of that type are accepted. Throwing a ''%%TypeError%%'' only if the keys of the arrays don't match is not in line with this RFC. The behavior of an operator should not depend on the value of the operand, only on the type. Furthermore, a ''%%TypeError%%'' would be misplaced here, as some arrays would be accepted but others not, whereas a ''%%TypeError%%'' indicates no values of that type are accepted.
  
-==== Why is switch affected? It's not an operator. ====+==== Why is switch not affected? ====
  
-Internally the ''%%case%%'' of a switch is handled as a comparison operatorThe issues with ''%%case%%'' are therefore similar to those of comparison operators. The audience that strict_operators caters tomay want to get rid of this behavior completely.+Using strict_operators will only result in cases where a ''TypeError'' is thrownIt will not affect the behaviorgiving a different result.
  
-This is determined by a secondary vote.+Changing ''switch'' to compare using ''==='' would change the behavior based on the directive.
  
-==== Are there cases where a statement doesn't throw a TypeError but yields a different result? ====+To use strict comparison, most ''switch'' statements can be rewritten to [[https://wiki.php.net/rfc/match_expression_v2|match statements]].
  
-No, not for operators.+==== Are there cases where a statement doesn't throw a TypeError but yields a different result? ====
  
-If the secondary vote for strict comparison with ''%%switch%%'' is accepted, ''%%switch%%'' can give a different result.+No.
  
 ==== Will this directive disable type juggling altogether? ==== ==== Will this directive disable type juggling altogether? ====
Line 114: Line 115:
 No. Operators can still typecast under the given conditions. For instance, the concatenation (''%%.%%'') operator will cast an integer or float to a string, and boolean operators will cast any type of operand to a boolean. No. Operators can still typecast under the given conditions. For instance, the concatenation (''%%.%%'') operator will cast an integer or float to a string, and boolean operators will cast any type of operand to a boolean.
  
-Typecasting is also done in other places: ''%%if%%'' and ''%%while%%'' statements interpret expressions as boolean, and booleans and floats are cast to an integer when used as array keys.+Type juggling is also done in other places: ''%%if%%'' and ''%%while%%'' statements interpret expressions as boolean, and booleans and floats are cast to an integer when used as array keys. The ''switch'' statement will still use loose comparison (''=='').
  
 This RFC is limited to the scope of operators. This RFC is limited to the scope of operators.
  
rfc/strict_operators/faq.1594043169.txt.gz · Last modified: 2020/07/06 13:46 by jasny