What does the b in front of string literals do?

前端 未结 3 1772
广开言路
广开言路 2020-11-29 11:04
$binary = b\'Binary string\';

What consequences does it have to create a string as binary?

I couldn\'t find any hint about tha

相关标签:
3条回答
  • 2020-11-29 11:25

    This is a forward compatibility token for the never-to-be-released PHP version 6, which should have had native unicode support.

    In PHP6, strings are unicode by default, and functions operate at the unicode character level on them. This "b" means "binary string", that is, a non unicode string, on which functions operate at the byte level.

    This has no effect in PHP != 6, where all strings are binary.

    0 讨论(0)
  • 2020-11-29 11:36

    Binary casting is available since 5.2.1 but will not take effect until 6.0 when unicode strings also take effect.

    Which explains why this does nothing special right now for me on a server using 5.2.6:

    <?php
    $t = b"hey";
    var_dump($t);
    //string(3) "hey"
    
    $s = (binary)"hey";
    var_dump($s);
    //string(3) "hey"
    ?>
    
    0 讨论(0)
  • 2020-11-29 11:38

    Convert to string

    $binary = preg_replace('/[[:^print:]]/', '', $binary);
    
    0 讨论(0)
提交回复
热议问题