Reads sorted-set members in ascending score order by index, with optional scores.
The objective is to understand the behavior and operational limits, not only the method name.
What It Does
Reads sorted-set members in ascending score order by index, with optional scores.
Syntax and Return Value
Redis::zRange(...)
The return value and exceptional cases depend on the call described here: The stop index is inclusive; use WITHSCORES or phpredis withscores option when needed.
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:zrange:1',5,"low",12,"high");
echo json_encode($redis->zRange('guide:zrange: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:zrange:2',5,"low",13,"high");
echo json_encode($redis->zRange('guide:zrange: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:zrange:3',5,"low",14,"high");
echo json_encode($redis->zRange('guide:zrange:3',0,-1,true));
Important Behavior and Edge Cases
The stop index is inclusive; use WITHSCORES or phpredis withscores option when needed.
For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.
Comparison with Related Approaches
| Approach | Important distinction |
|---|---|
| Redis::zRange | Reads sorted-set members in ascending score order by index, with optional scores. |
| Related API / pattern | zRevRange returns descending order, when available. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming the happy path covers edge cases | The stop index is inclusive; use WITHSCORES or phpredis withscores option when needed. |
| Using a related API interchangeably | zRevRange returns descending order, when available. |
Key Takeaways
- Reads sorted-set members in ascending score order by index, with optional scores.
- The stop index is inclusive; use WITHSCORES or phpredis withscores option when needed.
- zRevRange returns descending order, when available.
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.