rfc:strict_operators

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
rfc:strict_operators [2020/07/06 14:48]
jasny version 1.5 (doesn't affect switch)
rfc:strict_operators [2020/07/06 21:31]
jasny
Line 2: Line 2:
  
   * Version: 1.5   * Version: 1.5
-  * Date: 2020-06-09 (first version: 2019-05-25)+  * Date: 2020-07-06 (first version: 2019-05-25)
   * Author: Arnold Daniels, <jasny@php.net>   * Author: Arnold Daniels, <jasny@php.net>
   * Status: Under Discussion   * Status: Under Discussion
Line 23: Line 23:
 If strict_operators is enabled, the following stricter rules will be used; If strict_operators is enabled, the following stricter rules will be used;
  
-  * Operators may perform type casting, but not type juggling: +  * Operators may perform type conversion, but not type juggling: 
-    * Type casting is not based on the type of the other operand +    * Type conversion is not based on the type of any of the operands 
-    * Type casting is not based on the value of any of the operands+    * Type conversion is not based on the value of any of the operands
   * Operators will throw a ''%%TypeError%%'' for unsupported types   * Operators will throw a ''%%TypeError%%'' for unsupported types
  
-In case an operator can work with several (or all) types, the operands need to match as no casting will be done by those operators.+In case an operator can work with several (or all) types, the operand types need to match as no type conversion will be done by those operators.
  
 The one exception is that [[http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2|widening primitive conversion]] is allowed for ''%%int%%'' to ''%%float%%''. When doing a operation with an ''%%int%%'' and a ''%%float%%'', the ''%%int%%'' will silently casted to a ''%%float%%''. The one exception is that [[http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2|widening primitive conversion]] is allowed for ''%%int%%'' to ''%%float%%''. When doing a operation with an ''%%int%%'' and a ''%%float%%'', the ''%%int%%'' will silently casted to a ''%%float%%''.
 +
 +Using ''strict_operators'' never changes the outcome of an operation.
  
 ===== Details for operands ===== ===== Details for operands =====
Line 59: Line 61:
  
 When used with a ''%%bool%%'', ''%%string%%'', ''%%array%%'', ''%%object%%'', or ''%%resource%%'' operand, a ''%%TypeError%%'' will be thrown. When used with a ''%%bool%%'', ''%%string%%'', ''%%array%%'', ''%%object%%'', or ''%%resource%%'' operand, a ''%%TypeError%%'' will be thrown.
 +
 +Custom compare handlers of objects (like ''DateTime'' and ''gmp'') will be applied regardless of ''strict_operators''. The ''ZEND_COMPARE_OBJECTS_FALLBACK'' macro will throw a ''TypeError'' if two objects are compared with different handlers.
  
 ==== String concatenation ==== ==== String concatenation ====
Line 116: Line 120:
 This can lead to issues when numeric comparison is not expected, for example between two hexidecimal values. The hexidecimal value is instead interpreted as number with scientific notation. This can lead to issues when numeric comparison is not expected, for example between two hexidecimal values. The hexidecimal value is instead interpreted as number with scientific notation.
  
-<code>+<code php>
 $red = '990000'; $red = '990000';
 $purple = '9900e2'; $purple = '9900e2';
Line 149: Line 153:
 $a < $b; // true $a < $b; // true
 </code> </code>
- 
-The logic of relational operators other than ''%%==%%'', ''%%===%%'', ''%%!=%%'' and ''%%!==%%'' has limited practical use. 
  
 ==== Strict vs non-strict comparison of arrays ==== ==== Strict vs non-strict comparison of arrays ====
Line 192: Line 194:
 ===== FAQ ===== ===== FAQ =====
  
-This RFC has [[rfc/strict_operators/faq|FAQ]] that answers some questions+This RFC has an [[rfc/strict_operators/faq|FAQ]] that answers some questions
  
   * [[rfc/strict_operators/faq#what_has_been_changed_since_the_initial_proposal|What has been changed since the initial proposal?]]   * [[rfc/strict_operators/faq#what_has_been_changed_since_the_initial_proposal|What has been changed since the initial proposal?]]
Line 205: Line 207:
   * [[rfc/strict_operators/faq#are_built-in_functions_affected_by_strict_operators|Are built-in functions affected by strict_operators?]]   * [[rfc/strict_operators/faq#are_built-in_functions_affected_by_strict_operators|Are built-in functions affected by strict_operators?]]
   * [[rfc/strict_operators/faq#can_relational_operators_be_allowed_for_arrays|Can relational operators be allowed for arrays?]]   * [[rfc/strict_operators/faq#can_relational_operators_be_allowed_for_arrays|Can relational operators be allowed for arrays?]]
 +  * [[rfc/strict_operators/faq#why_is_switch_not_affected|Why is switch not affected?]]
   * [[rfc/strict_operators/faq#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?]]   * [[rfc/strict_operators/faq#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?]]
   * [[rfc/strict_operators/faq#will_this_directive_disable_type_juggling_altogether|Will this directive disable type juggling altogether?]]   * [[rfc/strict_operators/faq#will_this_directive_disable_type_juggling_altogether|Will this directive disable type juggling altogether?]]
rfc/strict_operators.txt · Last modified: 2020/07/15 14:27 by jasny