Content · Web Development · PHP

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

Tests whether one or more keys exist and returns the number present. For multiple keys the return is a count, not a boolean truth

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

Tests whether one or more keys exist and returns the number present.

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

What It Does


Tests whether one or more keys exist and returns the number present.

Syntax and Return Value


Redis::exists(...)

The return value and exceptional cases depend on the call described here: For multiple keys the return is a count, not a boolean truth value.

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:exists:1','draft');
echo $redis->exists('guide:exists: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->set('guide:exists:2','review');
echo $redis->exists('guide:exists:2');

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:exists:3','published');
echo $redis->exists('guide:exists:3');

Important Behavior and Edge Cases


For multiple keys the return is a count, not a boolean truth value.

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

Comparison with Related Approaches


ApproachImportant distinction
Redis::existsTests whether one or more keys exist and returns the number present.
Related API / patternget retrieves a string value, and can return false for an absent key.

Common Beginner Mistakes


MistakeCorrect approach
Assuming the happy path covers edge casesFor multiple keys the return is a count, not a boolean truth value.
Using a related API interchangeablyget retrieves a string value, and can return false for an absent key.

Key Takeaways


  • Tests whether one or more keys exist and returns the number present.
  • For multiple keys the return is a count, not a boolean truth value.
  • get retrieves a string value, and can return false for an absent key.

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.