Home >Backend Development >PHP Problem >How to convert numbers to binary in php
php method to convert numbers into binary: 1. Use the decbin() function, the syntax "decbin(number)"; 2. Use the base_convert() function, the syntax "bindec(number, 10, 2)" .
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php will change the number ( Decimal) to binary
1. Use the decbin() function
The decbin() function can convert decimal numbers into binary numbers.
<?php echo decbin(3) . "<br>"; echo decbin(1) . "<br>"; echo decbin(1587) . "<br>"; echo decbin(7); ?>
Output result:
11 1 11000110011 111
2. Use the base_convert() function
The base_convert() function can convert between any bases. Just set "bindec(number, 10, 2)".
<?php echo base_convert(2,10,2) . "<br>"; echo base_convert(1,10,2) . "<br>"; echo base_convert(1512,10,2) . "<br>"; echo base_convert(7,10,2); ?>
Output results:
10 1 10111101000 111
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert numbers to binary in php. For more information, please follow other related articles on the PHP Chinese website!