A good option for creating strings with binary data for saving (for example saving an sql statement to a file) into text files or php code is to do the following:
<?php
$field=bin2hex($field);
$field=chunk_split($field,2,"\\x");
$field= "\\x" . substr($field,0,-2);
?>
this will convert your field (binary or not) into hex and then convert the hex into a string which may be placed in a php file:
FFFFFFFF -> \xFF\xFF\xFF\xFF