rfc:binary_string_comparison

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
Next revisionBoth sides next revision
rfc:binary_string_comparison [2014/08/16 08:05] – examples for <, <=, >, >= maberfc:binary_string_comparison [2014/08/17 10:55] – sorting regular mabe
Line 17: Line 17:
 The current behavior leads to bugs that are very hard to find/catch and it makes code hard to know what's going on. The current behavior leads to bugs that are very hard to find/catch and it makes code hard to know what's going on.
  
-Using strict string comparison helps to workaround such behavior bit it ends up in using strict comparison all over which makes non-strict comparison useless.+Using strict string comparison helps to workaround such behavior bit it ends up in using strict comparison all over which makes non-strict comparison useless and some structures like the switch statement can't be used as it internally uses non-strict comparison. 
 + 
 +Since PHP 5.2.1 ''binary'' is an alias of the string type and a prefix ''b'' exists to mark a string binary which has no effect.
  
 ===== Proposal ===== ===== Proposal =====
Line 115: Line 117:
     true ('0.99999999999999995' <= '1')     true ('0.99999999999999995' <= '1')
     false ('0.99999999999999995' >= '1')     false ('0.99999999999999995' >= '1')
 +
 +=== binary marked strings (since PHP 5.2.1) ===
 +(http://3v4l.org/bWnUG)
 +
 +    <?php
 +    var_dump((binary)'1e1' == (binary)'10');
 +    var_dump(b'1e1' == b'10');
 +
 +Current Behavior (binary marked strings will be handled numerically):
 +
 +    bool(true)
 +    bool(true)
 +
 +Changed Behavior (all strings will be handled binary without a context):
 +
 +    bool(false)
 +    bool(false)
 +
 +=== sorting of strings ===
 +(http://3v4l.org/mA0Yq)
 +
 +    <?php 
 +    
 +    $arr = array('1', 3, 2, '03', '01', '02');
 +    
 +    echo "Sort regular:\n";
 +    sort($arr);
 +    var_dump($arr);
 +    
 +    echo "Sort numeric:\n"; 
 +    sort($arr, SORT_NUMERIC);
 +    var_dump($arr);
 +    
 +    echo "Sort binary:\n";
 +    sort($arr, SORT_STRING);
 +    var_dump($arr);
 +
 +Current Behavior:
 +
 +    Sort regular:
 +    array(6) {
 +      [0] =>
 +      string(2) "01"
 +      [1] =>
 +      string(1) "1"
 +      [2] =>
 +      string(2) "02"
 +      [3] =>
 +      int(2)
 +      [4] =>
 +      int(3)
 +      [5] =>
 +      string(2) "03"
 +    }
 +    Sort numeric:
 +    array(6) {
 +      [0] =>
 +      string(2) "01"
 +      [1] =>
 +      string(1) "1"
 +      [2] =>
 +      int(2)
 +      [3] =>
 +      string(2) "02"
 +      [4] =>
 +      string(2) "03"
 +      [5] =>
 +      int(3)
 +    }
 +    Sort binary:
 +    array(6) {
 +      [0] =>
 +      string(2) "01"
 +      [1] =>
 +      string(2) "02"
 +      [2] =>
 +      string(2) "03"
 +      [3] =>
 +      string(1) "1"
 +      [4] =>
 +      int(2)
 +      [5] =>
 +      int(3)
 +    }
 +
 +Changed Behavior:
 +
 +    Sort regular:
 +    array(6) {
 +      [0]=>
 +      string(2) "01"
 +      [1]=>
 +      string(2) "02"
 +      [2]=>
 +      string(1) "1"
 +      [3]=>
 +      int(2)
 +      [4]=>
 +      int(3)
 +      [5]=>
 +      string(2) "03"
 +    }
 +    Sort numeric:
 +    array(6) {
 +      [0]=>
 +      string(2) "01"
 +      [1]=>
 +      string(1) "1"
 +      [2]=>
 +      string(2) "02"
 +      [3]=>
 +      int(2)
 +      [4]=>
 +      string(2) "03"
 +      [5]=>
 +      int(3)
 +    }
 +    Sort binary:
 +    array(6) {
 +      [0]=>
 +      string(2) "01"
 +      [1]=>
 +      string(2) "02"
 +      [2]=>
 +      string(2) "03"
 +      [3]=>
 +      string(1) "1"
 +      [4]=>
 +      int(2)
 +      [5]=>
 +      int(3)
 +    }
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
rfc/binary_string_comparison.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1