Friday, September 5, 2025
HomeLanguagesPHP | hash_equals() Function

PHP | hash_equals() Function

The hash_equals function() is an inbuilt function in PHP which is used to compares two strings using the same time whether they are equal or not.
Syntax: 
 

hash_equals( $known_str, $usr_str )

Parameters: This function accept two parameters as mention above and describe below. 
 

  • $known_str: This parameter is used to specify the known length string.
  • $usr_str: This parameter is used to specify the user-supplied string.

Return Value: This function returns True if both strings are equal, False otherwise.
Below programs illustrate the hash_equals() function in PHP:
Program 1: 
 

php




<?php
 
// PHP program to illustrate
// hash_equals function
$known_str = crypt('GFG', 'Hello-GFG');
$usr_str   = crypt('GFG', 'Hello-GFG');
 
// Compare both strings
$res = hash_equals($known_str, $usr_str);
 
// Display result
var_dump($res);
?>


Output: 

bool(true)

 

Program 2: 
 

php




<?php
 
// PHP program to illustrate
// hash_equals function
$known_str = crypt('GFG', 'Hello-GFG');
$usr_str   = crypt('neveropen', 'Hello-GFG');
 
// Compare both strings
$res = hash_equals($known_str, $usr_str);
 
// Display result
var_dump($res);
?>


Output: 

bool(false)

 

Reference: http://php.net/manual/en/function.hash-equals.php
 

RELATED ARTICLES

Most Popular

Dominic
32267 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6635 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7026 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6720 POSTS0 COMMENTS