Home >Backend Development >PHP Problem >How to convert numbers to binary in php

How to convert numbers to binary in php

青灯夜游
青灯夜游Original
2021-10-08 18:29:474314browse

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)" .

How to convert numbers to binary in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn