Content · Web Development · PHP

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

Sets a key expiry in seconds and reports whether the operation succeeded. TTL is reset when you explicitly set expiry; nonexistent keys cannot be

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

Sets a key expiry in seconds and reports whether the operation succeeded.

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

What It Does


Sets a key expiry in seconds and reports whether the operation succeeded.

Syntax and Return Value


Redis::expire(...)

The return value and exceptional cases depend on the call described here: TTL is reset when you explicitly set expiry; nonexistent keys cannot be expired.

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->set('guide:expire:1','draft');
echo $redis->expire('guide:expire:1',60+3) ? "expiring" : "absent";

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->set('guide:expire:2','review');
echo $redis->expire('guide:expire:2',60+4) ? "expiring" : "absent";

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->set('guide:expire:3','published');
echo $redis->expire('guide:expire:3',60+5) ? "expiring" : "absent";

Important Behavior and Edge Cases


TTL is reset when you explicitly set expiry; nonexistent keys cannot be expired.

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

Comparison with Related Approaches


ApproachImportant distinction
Redis::expireSets a key expiry in seconds and reports whether the operation succeeded.
Related API / patternttl reports remaining lifetime; setex stores a value and TTL together.

Common Beginner Mistakes


MistakeCorrect approach
Assuming the happy path covers edge casesTTL is reset when you explicitly set expiry; nonexistent keys cannot be expired.
Using a related API interchangeablyttl reports remaining lifetime; setex stores a value and TTL together.

Key Takeaways


  • Sets a key expiry in seconds and reports whether the operation succeeded.
  • TTL is reset when you explicitly set expiry; nonexistent keys cannot be expired.
  • ttl reports remaining lifetime; setex stores a value and TTL together.

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.