Content · Web Development · PHP

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

Returns a list slice using inclusive start and stop indexes. Index -1 means the last element; stop is inclusive, unlike PHP array_slice

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Returns a list slice using inclusive start and stop indexes.

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

What It Does


Returns a list slice using inclusive start and stop indexes.

Syntax and Return Value


Redis::lRange(...)

The return value and exceptional cases depend on the call described here: Index -1 means the last element; stop is inclusive, unlike PHP array_slice length.

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->rPush('guide:lrange:1',"one","two",'draft');
echo json_encode($redis->lRange('guide:lrange:1',0,-1));

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->rPush('guide:lrange:2',"one","two",'review');
echo json_encode($redis->lRange('guide:lrange:2',0,-1));

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->rPush('guide:lrange:3',"one","two",'published');
echo json_encode($redis->lRange('guide:lrange:3',0,-1));

Important Behavior and Edge Cases


Index -1 means the last element; stop is inclusive, unlike PHP array_slice length.

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

Comparison with Related Approaches


ApproachImportant distinction
Redis::lRangeReturns a list slice using inclusive start and stop indexes.
Related API / patternlPop consumes values rather than reading a range.

Common Beginner Mistakes


MistakeCorrect approach
Assuming the happy path covers edge casesIndex -1 means the last element; stop is inclusive, unlike PHP array_slice length.
Using a related API interchangeablylPop consumes values rather than reading a range.

Key Takeaways


  • Returns a list slice using inclusive start and stop indexes.
  • Index -1 means the last element; stop is inclusive, unlike PHP array_slice length.
  • lPop consumes values rather than reading a range.

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.