Content · Web Development · PHP

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

Adds unique members to a Redis set. Repeated members are ignored; sets have no guaranteed iteration

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

Adds unique members to a Redis set.

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

What It Does


Adds unique members to a Redis set.

Syntax and Return Value


Redis::sAdd(...)

The return value and exceptional cases depend on the call described here: Repeated members are ignored; sets have no guaranteed iteration order.

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);
echo $redis->sAdd('guide:sadd:1',"php",'draft',"php");

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);
echo $redis->sAdd('guide:sadd:2',"php",'review',"php");

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

Important Behavior and Edge Cases


Repeated members are ignored; sets have no guaranteed iteration order.

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

Comparison with Related Approaches


ApproachImportant distinction
Redis::sAddAdds unique members to a Redis set.
Related API / patternzAdd assigns sortable scores to sorted-set members.

Common Beginner Mistakes


MistakeCorrect approach
Assuming the happy path covers edge casesRepeated members are ignored; sets have no guaranteed iteration order.
Using a related API interchangeablyzAdd assigns sortable scores to sorted-set members.

Key Takeaways


  • Adds unique members to a Redis set.
  • Repeated members are ignored; sets have no guaranteed iteration order.
  • zAdd assigns sortable scores to sorted-set members.

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.