PHP hash_final() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Hash

Definition

In PHP, hash_final() finishes an incremental hash that you started with hash_init() and returns the result.

After this call the context is closed, so the hash cannot be changed any more.

Syntax

Call hash_final() with the values you need. Start with this line.

PHP
$digest = hash_final($context);
CallWhat it does
hash_final($ctx)Returns the hash as a hex string.
hash_final($ctx, true)Returns the hash as raw bytes.

The result equals one hash() call on all the data you added.

Library declarations
PHP
hash_final(HashContext $context, bool $binary = false): string

Parameters

ParameterDescription
contextRequired. A HashContext from hash_init().
binaryOptional. false (default) returns hex. true returns raw bytes.

Return Value

Return ValueDescription
stringThe final hash as hex, or raw bytes if binary is true.

PHP Examples

Example 1 — Finish an Incremental Hash

Add two pieces, then finish.

PHP
<?php
$ctx = hash_init('sha256');
hash_update($ctx, 'Hello, ');
hash_update($ctx, 'World');

$digest = hash_final($ctx);

echo $digest, PHP_EOL;
echo "Same as hash(): ", $digest === hash('sha256', 'Hello, World') ? "yes" : "no", PHP_EOL;

How It Works

  1. Start a context. Pick the algorithm.
  2. Add two pieces. The order matters.
  3. Call `hash_final()`. It returns the hex hash.
  4. Compare with `hash()`. Both give the same value.

Example 2 — Raw Binary Result

Ask for bytes instead of hex.

PHP
<?php
$ctx = hash_init('sha256');
hash_update($ctx, 'Hello, World');

$raw = hash_final($ctx, true);

echo "Raw length: ", strlen($raw), PHP_EOL;
echo "Hex length: ", strlen(bin2hex($raw)), PHP_EOL;

How It Works

  1. Pass true as the second argument. You get bytes.
  2. Count the bytes. sha256 is 32 bytes.
  3. Convert to hex. bin2hex() makes 64 characters.
  4. Choose by need. Bytes suit storage, hex suits display.

Example 3 — A Finished Context Cannot Be Reused

Adding data after hash_final() throws an error.

PHP
<?php
$ctx = hash_init('sha256');
hash_update($ctx, 'data');
hash_final($ctx);

try {
    hash_update($ctx, 'more');
} catch (TypeError $e) {
    echo get_class($e), PHP_EOL;
}

How It Works

  1. Finish the context. It is now closed.
  2. Try to add more data. PHP rejects it.
  3. Catch the TypeError. The class name is printed.
  4. Use `hash_copy()` first if you need to continue.

Frequently Asked Questions

It ends an incremental hash and returns the finished hash value.
hash_final(HashContext $context, bool $binary = false): string.
No. The context is finished. To continue in two directions, use hash_copy() before you call hash_final().
Yes. Adding the data in pieces and then calling hash_final() gives the same hash as hash() on the joined data.
Pass true as the second argument. Use bin2hex() if you need to print the bytes.

Next: hash_hkdf()

hash_hkdf() derives secure keys from a strong secret.

hash_hkdf() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful