Working with Numbers

Working with Numbers

Welcome back to our Redis course! Now that you know how to connect to a Redis server using PHP, it's time to move forward and explore how to work with numbers. This unit builds on our previous lesson, so make sure you're comfortable with establishing a connection to a Redis server.

What You'll Learn

In this lesson, you will learn how to:

  1. Set numeric values.
  2. Retrieve and handle numeric values.

Here's the code snippet that we'll be working with:

PHP
<?php

require 'vendor/autoload.php';

use Predis\Client;

// Connect to Redis
$client = new Client([
    'scheme' => 'tcp',
    'host'   => '127.0.0.1',
    'port'   => 6379,
]);

// Setting numeric values
$client->set('count', 5);
$client->set('completion_rate', 95.5);

// Getting numeric values
$count = $client->get('count');
$completionRate = $client->get('completion_rate');

echo sprintf("Course count: %d, Completion rate: %.2f\n", $count, $completionRate);

?>

Let's Break Down the Code

Why It Matters

Working with numbers in Redis is crucial because many real-world applications involve numeric data. From tracking user statistics to monitoring system performance, managing numbers in Redis allows you to perform a variety of useful operations efficiently. By mastering these basic operations, you'll be well-prepared to tackle more complex tasks and optimize your applications.

Ready to dive in? Let's move on to the practice section and get hands-on experience working with numbers in Redis!

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal