PHP md5() Function


The md5() function calculates the MD5 hash of a string. MD5 (Message Digest Algorithm). The MD5 algorithm is intended for digital signature applications where large files need to be "compressed" in a secure manner before being encrypted with a private (private) key based on a public key cryptographic system such as RSA. The hash is returned as a 32-digit hexadecimal number.

Syntax
md5(string,raw)
Parameters
  • string - (Required) String to calculate.
  • raw - (Optional) Specifies hexadecimal or binary output format.
    • TRUE - 16 character raw binary format
    • FALSE - Default. 32 digit hexadecimal number

Example
<?php
$txt = "Welcome to the World";
echo md5($txt);
?>

Output

564eab13e01f908dc43e49dabc734cb6

Using raw parameter

Example
<?php
$str = "Welcome"."<br>";
        
// Output the value of $str, which is "Welcome" followed by a line break.
echo "$str"; 
        
// Output the MD5 hash of $str with binary output set to TRUE.
echo "TRUE - ".md5($str, TRUE)."<br>"; 
        
// Output the MD5 hash of $str with the default hexadecimal output.
echo "FALSE - ".md5($str);
?>

Output

Welcome
TRUE - ºšÓuÀ®A7¥-ª/
FALSE - 8dba069a0e1fd375c0ae4137a52daa2f

Prev Next