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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

sha1(string $string, bool $binary = false): string

Parameters

ParameterDescription
string The input string.
binary optional true returns the raw 20-byte digest. Default: false

Return value

The hash as a hex string.

Examples

Hash a stringTry it yourself
<?php
echo sha1("hello"), "\n";

Output

aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
Prefer SHA-256Try it yourself
<?php
echo hash("sha256", "hello"), "\n"; // the stronger choice

Output

2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

Version history

VersionChange
PHP 4.3Added

Was this page helpful?