How to Generate MD5 Hash in PHP

09/07/2021

Contents

In this article, you will learn how to generate MD5 hash in PHP.

Using the md5() function

The md5() function in PHP is used to generate an MD5 hash of a given string.

Syntax:
string md5 ( string $str [, bool $raw_output = false ] )
Parameters:
  • $str: The string to be hashed
  • $raw_output: An optional parameter that, if set to true, will return the hash as a raw binary string. If set to false or omitted, the hash will be returned as a 32-character hexadecimal string.
Example:
<?php
  $message = "This is the message to be hashed";
  $hash = md5($message);

  echo "The MD5 hash of '$message' is: $hash";
?>

This will generate a 32-character hexadecimal string, which is the MD5 hash of the input string.