w3resource

PHP: sha1() function

Description

The sha1() function is used to calculate the sha1 hash of a string.

Version:

(PHP 4 and above)

Syntax:

sha1(string1, raw_output)

Parameter:

Name Description Required /
Optional
Type
string1 The input string. Required String
raw_output If TRUE sets raw 20 characters binary format. If FALSE (default) sets raw 40 character hex number format. Optional Boolean

Return value:

Returns the sha1 hash as a string.

Value Type: String.

Example:

<?php
$str = 'Good Morning';
echo sha1($str).'<br>';
echo sha1($str,TRUE).'<br>';
?>

Output:

06e336231466d0b0573a05cbca7813444afb5b29
ã6#fаW:ËÊxDJû[)

View the example in the browser

See also

PHP Function Reference

Previous: sha1_file
Next: similar_text



PHP: Tips of the Day

In PHP, there are two versions of logical AND and OR operators.

Operator True if
$a and $b Both $a and $b are true
$a && $b Both $a and $b are true
$a or $b Either $a or $b is true
$a || $b Either $a or $b is true

Note that the && and || opererators have higher precedence than and and or. See table below:

Evaluation Result of $e Evaluated as
$e = false || true True $e = (false || true)
$e = false or true False ($e = false) or true

Because of this it's safer to use && and || instead of and and or.