w3resource

PHP: bin2hex() function

Description

The bin2hex() function converts a string of binary characters to hexadecimal values.

Version:

(PHP 4 and above)

Syntax:

bin2hex(string_name)

Parameter:

Name Description Required /
Optional Type string_name The string to be converted. Required String

Return values:

The hexadecimal representation of the given string.

Value Type: String.

Pictorial Presentation

php-string-bin2hex()

Example:

<?php
 $string_name='w3resource.com';
 echo bin2hex($string_name);
?>

Output:

77337265736f757263652e636f6d 

View the example in the browser

See also

PHP Function Reference

Previous: addcslashes
Next: chop



PHP: Tips of the Day

Deep flattens an array

Example:

<?php
function tips_deepFlatten($items)
{
  $result = [];
  foreach ($items as $item) {
    if (!is_array($item)) {
      $result[] = $item;
    } else {
      array_push($result, ...tips_deepFlatten($item));
    }
  }

  return $result;
}

Print_r(tips_deepFlatten([1, [2], [[3], 4], 5]));
?>

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)