- URL: https://www.laruence.com/en/2011/10/10/2232.html
- Please include attribution when republishing.
Starting with PHP 5.4, we can directly write binary literals in code. This is especially convenient when defining some flag bits.
Look at the example below:
$bin = 0b1101; echo $bin; //13
In the past, we needed to use bin2dec to represent it as a string, which was really unpleasant.
In addition, PHP 5.4 also added hex2bin, which can directly convert a hex-represented string into a binary data stream. In the past, we needed to use pack to do this.
<?php
$hex = "ff0f";
var_dump(hex2bin($hex) === pack("H*", $hex));
//bool(true);
That said, a reminder: PHP 5.4 is still under development. Before the final release, any new feature may be adjusted or changed. If you have any suggestions, feedback is welcome, to help make PHP even better.
Thanks
For more updates, follow: Changelog
Be First to Comment