PHP hash_update() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Hash

Definition

In PHP, hash_update() adds a piece of data to a hashing context that was started with hash_init().

Call it as many times as you need, then finish with hash_final().

Syntax

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

PHP
hash_update($context, $data);
CallWhat it does
hash_update($ctx, 'Hello, ')Adds a piece of text.
hash_update($ctx, 'World')Adds the next piece.

Pieces are added in order. Several calls give the same result as one hash() call on the joined text.

Library declarations
PHP
hash_update(HashContext $context, string $data): bool

Parameters

ParameterDescription
contextRequired. A HashContext from hash_init().
dataRequired. The string to add.

Return Value

Return ValueDescription
boolAlways true in PHP 8. A finished context throws a TypeError.

PHP Examples

Example 1 — Add Data in Pieces

Two updates equal one hash() call.

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. Choose sha256.
  2. Add the first piece. Hello,.
  3. Add the second piece. World.
  4. Finish and compare. The hash equals the one-shot result.

Example 2 — The Return Value

It returns true on success.

PHP
<?php
$ctx = hash_init('sha256');

var_dump(hash_update($ctx, 'x'));

How It Works

  1. Start a context. It is ready for data.
  2. Call `hash_update()`. It adds x.
  3. Dump the result. It is true.
  4. Skip the check. In PHP 8 it never returns false.

Example 3 — Hash Text in Fixed Chunks

Split a string and feed the parts one at a time.

PHP
<?php
$text = 'The quick brown fox jumps over the lazy dog';

$ctx = hash_init('sha256');
foreach (str_split($text, 8) as $chunk) {
    hash_update($ctx, $chunk);
}

echo "Same as hash(): ", hash_final($ctx) === hash('sha256', $text) ? "yes" : "no", PHP_EOL;

How It Works

  1. Split the text. str_split() makes 8-character chunks.
  2. Update in a loop. Each chunk is added in order.
  3. Finish. The context holds the whole text.
  4. Compare. Chunking never changes the result.

Frequently Asked Questions

It adds data to a hashing context started with hash_init(). The hash is not finished until you call hash_final().
hash_update(HashContext $context, string $data): bool.
No. The pieces are joined in order, so the final hash equals hash() on the full string.
true. In PHP 8 it always returns true or throws an error.
PHP throws a TypeError, because hash_final() closes the context.

Next: hash_update_file()

hash_update_file() adds a file's contents to an incremental hash.

hash_update_file() 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