Content · Programming · PHP

PHP Redis::zAdd() Method: Usage and Examples

Adds or updates members of a sorted set with numerical scores. Equal-score tie ordering is lexicographic, not arrival order; flags may change update

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

Adds or updates members of a sorted set with numerical scores.

The objective is to understand the behavior and operational limits, not only the method name.

What It Does


Adds or updates members of a sorted set with numerical scores.

Syntax and Return Value


Redis::zAdd(...)

The return value and exceptional cases depend on the call described here: Equal-score tie ordering is lexicographic, not arrival order; flags may change update semantics.

Requirements: install and enable the corresponding PHP driver or extension and configure the service before running the examples.

Example 1: Basic Use


Requires the phpredis extension and a running Redis server. Keys use a guide-specific prefix; clean up example keys after experimenting.

<?php
$redis = new Redis();
$redis->connect(getenv('REDIS_HOST') ?: '127.0.0.1', (int) (getenv('REDIS_PORT') ?: 6379), 2.0);
$redis->zAdd('guide:zadd:1',3,"taskA",7,"taskB");
echo json_encode($redis->zRange('guide:zadd:1',0,-1,true));

Example 2: A Second Scenario


Requires the phpredis extension and a running Redis server. Keys use a guide-specific prefix; clean up example keys after experimenting.

<?php
$redis = new Redis();
$redis->connect(getenv('REDIS_HOST') ?: '127.0.0.1', (int) (getenv('REDIS_PORT') ?: 6379), 2.0);
$redis->zAdd('guide:zadd:2',4,"taskA",8,"taskB");
echo json_encode($redis->zRange('guide:zadd:2',0,-1,true));

Example 3: Practical Checks


Requires the phpredis extension and a running Redis server. Keys use a guide-specific prefix; clean up example keys after experimenting.

<?php
$redis = new Redis();
$redis->connect(getenv('REDIS_HOST') ?: '127.0.0.1', (int) (getenv('REDIS_PORT') ?: 6379), 2.0);
$redis->zAdd('guide:zadd:3',5,"taskA",9,"taskB");
echo json_encode($redis->zRange('guide:zadd:3',0,-1,true));

Important Behavior and Edge Cases


Equal-score tie ordering is lexicographic, not arrival order; flags may change update semantics.

For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.

Comparison with Related Approaches


ApproachImportant distinction
Redis::zAddAdds or updates members of a sorted set with numerical scores.
Related API / patternsAdd creates an unordered unique set with no scores.

Common Beginner Mistakes


MistakeCorrect approach
Assuming the happy path covers edge casesEqual-score tie ordering is lexicographic, not arrival order; flags may change update semantics.
Using a related API interchangeablysAdd creates an unordered unique set with no scores.

Key Takeaways


  • Adds or updates members of a sorted set with numerical scores.
  • Equal-score tie ordering is lexicographic, not arrival order; flags may change update semantics.
  • sAdd creates an unordered unique set with no scores.

Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.

Share this resourceSend this page to someone who may find it useful.