hash calculates a digest for one input string using a selected hashing algorithm, returning hex by default or raw bytes with the binary option.
The examples explain the data format, expected results and the difference between this API and nearby cryptographic operations.
What It Does
The operation is intended for: hash calculates a digest for one input string using a selected hashing algorithm, returning hex by default or raw bytes with the binary option.
Part of core PHP or a normally bundled standard extension; check your deployment configuration.
Syntax and Return Value
hash(string $algo, string $data, bool $binary = ..., array $options = ...): string
| Parameter | Purpose |
|---|---|
| $algo (string) | The exact algorithm identifier supported by this API. |
| $data (string) | The input data bytes. Use the exact original bytes when verifying. |
| $binary (bool) | Use the PHP signature above to determine the exact expected input type; validate untrusted inputs before calling. |
| $options (array) | Driver or algorithm-specific options; avoid unchecked user-controlled settings. |
The declared return type is shown above. Errors can include false results or exceptions depending on this API: An ordinary hash is not encryption and is not a password-storage function. Use HMAC when a shared-secret authenticity check is needed.
Example 1: Basic Operation
Inspect the expected result: do not print secrets or reuse illustrative credentials in production.
<?php
$digest = hash("sha256", "invoice-42");
echo $digest;
Example 2: A Different Use Case
Inspect the expected result: do not print secrets or reuse illustrative credentials in production.
<?php
// Check the alternative case.
$digest = hash("sha256", "approved-record");
echo $digest;
Practical Example: Checking the Result
Inspect the expected result: do not print secrets or reuse illustrative credentials in production.
<?php
// Verify the expected behavior before using results.
$digest = hash("sha256", "new-session");
echo $digest;
Important Behavior and Edge Cases
An ordinary hash is not encryption and is not a password-storage function. Use HMAC when a shared-secret authenticity check is needed.
An ordinary hash is not encryption and is not a password-storage function. Use HMAC when a shared-secret authenticity check is needed.
Comparison with Related Functions
| Approach | Key distinction |
|---|---|
| hash | hash calculates a digest for one input string using a selected hashing algorithm, returning hex by default or raw bytes with the binary option. |
| Related technique | hash() computes a digest in one call; hash_init/hash_update/hash_final support incremental input. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Confusing this operation with a neighboring cryptographic or security mechanism | hash() computes a digest in one call; hash_init/hash_update/hash_final support incremental input. |
| Ignoring a security prerequisite or failure case | An ordinary hash is not encryption and is not a password-storage function. Use HMAC when a shared-secret authenticity check is needed. |
Key Takeaways
- hash calculates a digest for one input string using a selected hashing algorithm, returning hex by default or raw bytes with the binary option.
- An ordinary hash is not encryption and is not a password-storage function. Use HMAC when a shared-secret authenticity check is needed.
- hash() computes a digest in one call; hash_init/hash_update/hash_final support incremental input.
Apply this behavior to a real application only after confirming the required extension, configuration and failure handling on the deployment runtime.