We now have a youtube channel. Subscribe!

PHP | hash_init() Function

PHP hash_init() Function

Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP hash_init() Function.

The built-in hash_init() function initializes an incremental hashcontext which can be used with other PHP hash functions such as hash_update(), hash_final() etc. It takes input as a hash algorithm and output as a hash context.

A hashcontext is generated based on the hash_algorithm used inside the hash_init(). You can update your data or message with the hashcontext using the hash_update() function and then get the final hash using the hash_final() function.


Following below is the syntax to use this function -

hash_init ( string $algo [, int $options = 0 [, string $key = NULL ]] ) : HashContext

Parameter Details

Sr.NoParameter & Description


Name of the hashing algorithm. There is a big list of algorithm available with hash, some important ones are md5, sha256, etc.

To get the full list of algorithms supported use the hashing function hash_algos()



There is only one option supported and that is HASH_HMAC. If you are using options, the key is also mandatory.



If HASH_HMAC is used as an option, the key also has to be given and it will be a shared secret key that will be used with HMAC hashing method.

Return Value

It returns a hashing context. The hashing context can be used with other functions like the hash_update(), hash_update_file(), hash_final() and hash_update_stream().

PHP Version

This built-in PHP function works from PHP version greater than 5.1.2.


The following example illustrates how to generate a hashing context -

   $hash_context = hash_init('md5');
   hash_update($hash_context, 'Testing php');
   hash_update($hash_context, ' hash functions.');
   echo hash_final($hash_context);


When the above code is executed, it will produce the following result -



The following example illustrates the usage of the built-in PHP hash_init() function with the hash_copy() -

   $hash_context = hash_init("md5");
   hash_update($hash_context, "Welcome To Webdesigntutorialz");
   $hash_copy= hash_copy($hash_context);
   echo hash_final($hash_context);
   echo "<br/>";
   hash_update($hash_copy,  "Welcome To Webdesigntutorialz");
   echo hash_final($hash_copy);


When the above code is executed, it will produce the following result -


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be studying about the PHP hash_update() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain