PHP 8 reference · String Functions
PHP sha1() Function
Calculate the SHA-1 hash of a string as 40 hex characters. Weak for security; prefer hash("sha256", ...).
PHP 8 Since 4.3
Syntax
sha1(string $string, bool $binary = false): string
Parameters
| Parameter | Description |
|---|---|
string |
The input string. |
binary optional |
true returns the raw 20-byte digest. Default: false |
Return value
The hash as a hex string.
Examples
<?php
echo sha1("hello"), "\n";
Output
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
<?php
echo hash("sha256", "hello"), "\n"; // the stronger choice
Output
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
Version history
| Version | Change |
|---|---|
| PHP 4.3 | Added |
Related
md5()· Calculate the MD5 hash of a string as 32 hex characters. Fine for checksums and cache keys, never for passwords.hash()· Hash a string with the algorithm you choose, such as sha256, sha512, crc32b or xxh3. Returns a hex string.- PHP Strings · tutorial