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

How to convert numbers to binary in php

青灯夜游
Release: 2023-03-13 08:22:02
Original
4300 people have browsed it

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);
?>
Copy after login

Output result:

11
1
11000110011
111
Copy after login

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);
?>
Copy after login

Output results:

10
1
10111101000
111
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template