Overview
KB
Technical FAQ
PHP Manual
CSS2 Manual
HTML Manual
JS Guide
JS Reference
PhpDock Manual
Nu-Coder Manual
PhpExpress Manual
PHP Joomla
Development
Learn PHP
 
<sodium_incrementsodium_memzero>
^Sodium FunctionsLast updated: Mon, 26 Oct 2026

sodium_memcmp

(PHP 7 >= 7.2.0, PHP 8)

sodium_memcmp — Test for equality in constant-time

Description

function sodium_memcmp(#[\SensitiveParameter] string $string1, #[\SensitiveParameter] string $string2): int

Compare two strings in constant-time.

In practice, you almost always want to use hash_equals() instead, since it provides the same logic but returns a bool instead of an int. However, if you're using the return value of a comparison in a calculation that's timing-sensitive, and worried about timing leaks with bool-to-int conversions, sodium_memcmp() is an ideal replacement.

Parameters

string1

String to compare

string2

Other string to compare

Return Values

Returns 0 if both strings are equal; -1 otherwise.


<sodium_incrementsodium_memzero>
^Sodium FunctionsLast updated: Mon, 26 Oct 2026