PHP md5() Function

Usage — The PHP md5() function is used to calculate the md5 hash of a given string.

It has the following syntax:

PHP

string md5 ( string $str [, bool $raw_output = false ] )

Here is an example of using md5():

PHP

$str = "PHP";

// Output — 2fec392304a5c23ac138da22847f9b7c
echo md5($str);

Return Value — This function returns the hash as a 32-character hexadecimal number.

Additional Information — If you want to calculate the MD5 hash of a file, you should use the md5_file() function instead.

This function calculates the MD5 hash of $str using the RSA Data Security, Inc. MD5 Message-Digest Algorithm, and returns that hash.

PHP Version and Changelog — The md5() function is available in PHP 4, PHP 5, PHP 7. The raw_output parameter became optional in PHP 5.0.

Relevant Functions — Other related PHP functions that you should know about are: md5_file() which calculates the md5 hash of a given file, sha1_file() which calculates the sha1 hash of a file, crc32() which calculates the crc32 polynomial of a string, sha1() which calculates the sha1 hash of a string, hash which generates a hash value (message digest), crypt() which does one-way string hashing and password_hash which creates a password hash.

Go Directly To — usage, parameters, working examples or additional tips.

Parameters

str

The str parameter is used to specify the string whose hash needs to be calculated. This is a required parameter.

raw_output

The raw_output parameter is used to specify if the digest should be returned in raw binary format with 16 characters or as a 32 character Hex number. This is an optional parameter and its default value is false.

Working Examples

Here are some examples of using the md5() function:

PHP

$str = 'apple';
if (md5($str) === '1f3870be274f6c49b3e31a0c6728957f') {
    echo "Would you like a green or red apple?";
}

Further Reading

  1. You can read more about the PHP md5() function on PHP.net.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top