Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the prefix for binary in PHP?

Tags:

syntax

php

binary

It's neither 0x nor 0; what is it? Is there?

like image 579
user198729 Avatar asked Mar 24 '10 11:03

user198729


People also ask

What is binary in PHP?

PHP provides us with a built-in function, decbin() for this purpose. The decbin() function in PHP is used to return a string containing a binary representation of the given decimal number argument. decbin stands for decimal to binary. Syntax: string decbin(value)

What does 0b0 mean in binary?

'0b' is used to tell the computer that the number you typed is a base-2 number not a base-10 number. Submitted by Omar Zaffar Khan.

Can there be exceptions to binary?

Worldwide, there are many individuals and several subcultures that can be considered exceptions to the gender binary or specific transgender identities. In addition to individuals whose bodies are naturally intersex, there are also specific ceremonial and social roles that are seen as third gender.


2 Answers

As of PHP 5.4+ the prefix for binary number is:

0b 


For ealier version, there is no such prefix. Instead, you can use 0x, for hexadecimal.

For more informations, see the Integers section of the PHP manual.

Still, if you really need to write values using binary before PHP 5.4, you can use the bindec function, that takes a string containing the binary, and returns the corresponding value.

For example, the following portion of code :

echo bindec('10011'); 

Will get you :

19 

But note you shouldn't do that too often : calling a function to do that each time the script is executed is quite bad for performances ^^
Instead, it's really a better solution to write your values using hexadecimal, where each digit codes for 4 bits.

like image 50
Pascal MARTIN Avatar answered Sep 24 '22 00:09

Pascal MARTIN


New in PHP 5.4 is the binary prefix, 0b. Think about portability when using it though; you'll need to be able to guarantee the server runs PHP 5.4+.

like image 21
Brad Koch Avatar answered Sep 22 '22 00:09

Brad Koch