crypt() Function in PHP 8.2, PHP 8.3 & PHP 8.4


The `crypt()` function in PHP is used for one-way string hashing (password hashing). In PHP 8.2, it works similarly to previous versions PHP 8, PHP 8.1, PHP 8.2, PHP 8.3 and PHP 8.4.Basic Example
<?php$password = 'mySecurePassword123';$hashedPassword = crypt($password);echo "Hashed password: " . $hashedPassword;// Verify a password$userInput = 'mySecurePassword123';if (hash_equals($hashedPassword, crypt($userInput, $hashedPassword))) {echo "\nPassword matches!";} else {echo "\nInvalid password!";}?>
Modern Alternative (Recommended)In PHP 8.2, it's better to use the `password_hash()` and `password_verify()` functions:
<?php$password = 'mySecurePassword123';$hashedPassword = password_hash($password, PASSWORD_DEFAULT);echo "Hashed password: " . $hashedPassword;// Verifyif (password_verify($password, $hashedPassword)) {echo "\nPassword is valid!";} else {echo "\nInvalid password!";}?>
Important Notes for PHP 8.21. The `crypt()` function is still available but considered legacy2. For new projects, always prefer `password_hash()` with `PASSWORD_DEFAULT` or `PASSWORD_BCRYPT`3. If using `crypt()`, always provide a salt and use strong algorithms like Blowfish (`$2y$`)The `password_hash()` function is simpler to use and automatically handles salt generation using a cryptographically secure method.