What does the b in front of string literals do?

Binary casting is available since 5.2.1 but will not take effect until 6.0 when unicode strings also take effect.

Which explains why this does nothing special right now for me on a server using 5.2.6:

<?php
$t = b"hey";
var_dump($t);
//string(3) "hey"

$s = (binary)"hey";
var_dump($s);
//string(3) "hey"
?>

This is a forward compatibility token for the never-to-be-released PHP version 6, which should have had native unicode support.

In PHP6, strings are unicode by default, and functions operate at the unicode character level on them. This "b" means "binary string", that is, a non unicode string, on which functions operate at the byte level.

This has no effect in PHP != 6, where all strings are binary.

Tags:

Php

String

Syntax