PHP Function Reference

PHP hash() Function



The PHP hash() function is used to generate a hash value (message digest) for the given data based on the algorithm like "md5", "sha256".

A message digest is a hash with lowercase hexits (lowercase hexidecimals) that is generated using the hash algorithms. It is mainly used to secure the data so that the message or data send is not changed.

Syntax

hash(algo, data, binary)

Parameters

context Required. Specify the name of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", etc..). A list of supported algorithms can be found using hash_algos() function.
data Required. Specify the message to be hashed.
binary Optional. If set to true, outputs raw binary data. Default is false which outputs lowercase hexits.

Return Value

Returns a string containing the calculated message digest as lowercase hexits unless binary is set to true in which case the raw binary representation of the message digest is returned.

Example: hash() example

The example below shows the usage of hash() function.

<?php
//generating a hash value of the 
//given message and displaying it
echo hash('md5', 'Hello World!');
?>

The output of the above code will be:

ed076287532e86365e841e92bfc50d8c

❮ PHP Hash Reference