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() Function.
The built-in PHP hash() function returns a hash value for the given data based on the algorithm like (md5, sha256). The return value is a string with hexits (hexadecimal values).
The built-in PHP hash() function returns a hash value for the given data based on the algorithm like (md5, sha256). The return value is a string with hexits (hexadecimal values).
Syntax
Following below is the syntax to use this function -
hash ( string $algo , string $data [, bool $raw_output = FALSE ] ) : string
READ: PHP | Hash Functions
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | algo Name of the hashing algorithm. There is a big list of algorithm available with hash, some important ones are md5, sha256, etc. |
2 | data The data you want the hash to be generated. Please note once the hash is generated it cannot be reversed. |
3 | raw_output By default, the value is false and hence it returns lowercase hexits values. If the value is true, it will return raw binary data. |
Return Value
This built-in PHP function returns a string with lowercase hexits. If the raw_output is set to true, it will return raw binary data.
PHP Version
This built-in PHP function works from PHP version greater than 5.1.2.
Example1
Following below is an example to generate a hash value using the md5 Algorithm -
<?php echo "The hash of Welcome to Webdesigntutorialz is - ". hash('md5', 'Welcome to Webdesigntutorialz'); ?>
Output
When the above code is executed, it will produce the following result -
The hash of Welcome to Webdesigntutorialz is - 98c299a76c4b81497677fc5aa861166d
Example2
Following below is an example to generate a hash value using the sha256 Algorithm -
<?php echo "The hash of Welcome to Webdesigntutorialz is - ". hash('sha256', 'Welcome to Webdesigntutorialz'); ?>
Output
When the above code is executed, it will produce the following result -
The hash of Welcome to Webdesigntutorialz is - b952e8666ddb57f4b80a70041a9bc151166dd11d4aaf1393c243cf720841be77
Example3
Following below is an example to generate a hash value using the crc32b Algorithm -
<?php echo "The hash of Welcome to Webdesigntutorialz is - ". hash('crc32b', 'Welcome to Webdesigntutorialz'); ?>
Output
When the above code is executed, it will produce the following result -
The hash of Welcome to Webdesigntutorialz is - d4a982a4
Example4
Following below is an example to generate a hash value with the raw_output set to true -
<?php echo "The hash of Welcome to Webdesigntutorialz is - ". hash('md5', 'Welcome to Webdesigntutorialz', true); ?>
Output
When the above code is executed, it will produce the following result -
The hash of Welcome to Webdesigntutorialz is - ��#�x"�%�������
READ: PHP | unlink() Function
Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP hash_hmac_file() 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 tutorial posts delivered directly to your emails.
Thanks for reading and bye for now.
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 tutorial posts delivered directly to your emails.
Thanks for reading and bye for now.