Is Binary cast available in PHP?

Asked

Viewed 102 times

3

PHP has a cast (that until then I did not know), called binary.

Example of Handbook:

$binary = (binary)$string;
$binary = b"binary string";

According to the PHP Handbook:

(Binary) - converts to binary string

I saw that a user still posted the following:

Cast a string to Binary using PHP < 5.2.1

(convert a string to binary in versions prior to PHP 5.2.1)

$binary = unpack('c*', $string);

With the test I performed in PHP 5.6, the result was as follows::

$string = "My String";

$binary = (binary)$string;
$binary = b"binary string";

var_dump($binary); // string(13) "binary string"

That is, returns the same value as the string (even with the cast for binary).

The question is: is this functionality really available (being bug of my PHP) or it will just be a future implementation (as they say: "In PHP 6")?

  • As it says in the manual: (Binary) - converts to binary string (PHP 6)

  • 3

    @Jorgeb. only the doc in Portuguese is like this.

  • @Jorgeb. It is because there is information where a user says: _"PHP 5.2.1 <". That is, it implies that it should already work

  • That’s right, @gmsantos. And there’s more "(Binary) casting and b prefix forward support was Added in PHP 5.2.1" it seems to me to sound like _"prefix support has been implemented binary, but it won’t work yet". kkkkk

  • 1

    Here it seems that nothing happens to the string: http://sandbox.onlinephpfunctions.com/code/392dde9694e267a9d4e2e687e602e21097faa725

  • 1

    Let’s see now an official response. Let’s see what the manual people have to tell us in this Bug Reporting

Show 1 more comment

2 answers

3

First the manual says it will be available in PHP 6 (which is a version that died). Or another version since even in this PHP is a mess. So you can’t test it now.

This kind of operation shouldn’t be a cast. But it’s PHP, we should be used to it by now.

You are changing the value obtained with the cast. I can’t imagine why. So it gave the result shown. If you take the new assignment after the cast, in theory would give the expected result if still correct another error.

There are no real examples of how this will work so it’s hard to understand what the shape will be, I’ll guess here what might be:

$string = "10011101";
$binary = (binary)$string;
$binary = b"10011101";

In both cases the number will be an integer worth 157. I imagine.

In PHP 5.2.1 you can do it another way, not this way.

I’ve already told you about the quality of the manual, especially the Portuguese version. This may not even be present in the next version of PHP. It may be that this was discussed but never decided to put in the language. Do not rely on speculation.

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Although I really like the PHP, sometimes I think he has a lot of gambit and a lot of stuff that doesn’t follow a pattern. Example: isset and is_null, for me, I should follow a writing pattern like isset and isnull. Among many other things!

  • @bigown the problem is that in the manual EN says it is already working.

  • 2

    @Wallacemaxters going around we’d have talk for months or years ;)

  • 2

    I liked PHP until I found out it’s a disgrace. @Jorgeb. I saw it later, but it’s not working according to all the tests I did. That is, the syntax works, but does not produce http://ideone.com/CnTAQE results

  • I’ll call it off. I already know from my own experience that PHP has these things: Create a resource that, however, is not yet implemented (for those who do not know, it is the same for constant INPUT_SESSION in the implementation of filter_input). Look at this gambiarra

  • Yes @bigown got exactly the same results. PHP has a mountain of these details that are a complete disgrace. It also has the advantage that, without well used (conscious user), be a very good tool!

  • @Wallacemaxters there are more cases in filter_input that do not work.

  • 2

    @wallacemaxters if (Binary) does not work as it should have tried to inform as a bug?

  • Not yet! but it’s a great idea. Maybe this will help in the progress (we complain so much) of the language!

  • @Wallacemaxters think it would fit as a new answer explaining why it doesn’t work or the right use.

Show 5 more comments

1


Following @gmsantos' instructions, I created this Bug Reporting there in PHP.

According to the response of the user identified as [email protected], the reason for the binary not function as expected is as follows:

(Binary) is a forward Compatibility token for the now abandoned "PHP 6". It behaves the same way as Doing a (String) Cast in "PHP 5".

If anyone can help me translate, thank you. But I will give an interpretation (and not a translation):

This is a keyword that was added in advance to the (now abandoned) PHP 6. The cast for binary behaves in the same way as cast to string in PHP 5.

  • 1

    Perhaps this will be added in the future, or a reservation will be made in the documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.