How does the bin2hex() function work in PHP?


It is a function that helps in converting a string into a hexadecimal value. It is done byte wise and the high nibbled bytes are converted first.

Syntax of bin2hex() function

bin2hex($string)

It takes in a string and converts the string to a hexadecimal representation.

Example

 Live Demo

<?php
   $my_str1 = "This";
   $my_str2 = "is";
   $my_str3 = "a";
   $my_str4 = "sample";
   print(bin2hex($my_str1));
   echo "<br>";
      print(bin2hex($my_str2));
   echo "<br>";
      print(bin2hex($my_str3));
   echo "<br>";
      print(bin2hex($my_str4));
?>

Output

54686973
6973
61
73616d706c65

Within the <php> tags, the bin2hex function is called on 4 elements which are previously defined. Then, the result of calling the bin2hex function on each of the string is displayed on the console.

Updated on: 01-Jul-2020

82 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements