# Connecting with PHP

This guide explains how to establish a connection between a PHP application and a KeyDB database using the <span class="s3">phpredis</span> extension. It walks through the necessary setup, configuration, and execution of a simple KeyDB command.

## **Variables**

Certain parameters must be provided to establish a successful connection to a KeyDB database. Below is a breakdown of each required variable, its purpose, and where to find it. Here’s what each variable represents:

<table border="1" id="bkmrk-variable-description" style="width: 100%; border-collapse: collapse; border-color: rgb(0, 0, 0);"><thead><tr><th style="width: 11.3214%; border-color: rgb(0, 0, 0);">**Variable**

</th><th style="width: 42.6714%; border-color: rgb(0, 0, 0);">**Description**

</th><th style="width: 46.1263%; border-color: rgb(0, 0, 0);">**Purpose**

</th></tr></thead><tbody><tr><td style="width: 11.3214%; border-color: rgb(0, 0, 0);">`HOST`

</td><td style="width: 42.6714%; border-color: rgb(0, 0, 0);">KeyDB hostname, from the Elestio service overview page

</td><td style="width: 46.1263%; border-color: rgb(0, 0, 0);">The address of the server hosting your KeyDB instance.

</td></tr><tr><td style="width: 11.3214%; border-color: rgb(0, 0, 0);">`PORT`

</td><td style="width: 42.6714%; border-color: rgb(0, 0, 0);">Port for KeyDB connection, from the Elestio service overview page

</td><td style="width: 46.1263%; border-color: rgb(0, 0, 0);">The network port used to connect to KeyDB. The default port is 6379.

</td></tr><tr><td style="width: 11.3214%; border-color: rgb(0, 0, 0);">`PASSWORD`

</td><td style="width: 42.6714%; border-color: rgb(0, 0, 0);">KeyDB password, from the Elestio service overview page

</td><td style="width: 46.1263%; border-color: rgb(0, 0, 0);">The authentication key required to connect securely to KeyDB.

</td></tr></tbody></table>

These values can usually be found in the Elestio service overview details as shown in the image below. Make sure to take a copy of these details and add it to the code moving ahead.

[![Screenshot 2025-06-26 at 1.13.23 PM.jpg](https://docs.elest.io/uploads/images/gallery/2025-06/scaled-1680-/C6oscreenshot-2025-06-26-at-1-13-23-pm.jpg)](https://docs.elest.io/uploads/images/gallery/2025-06/C6oscreenshot-2025-06-26-at-1-13-23-pm.jpg)

## **Prerequisites**

- **Install PHP**
    - Check if PHP is installed by running:

```
php -v
```

- - If not installed, download it from [php.net](https://www.php.net/downloads) and install.
- **Install the phpredis Extension**
    - The <span class="s2">phpredis</span> extension provides a native PHP interface for KeyDB. You can install it using:

```
sudo pecl install redis
```

- - Then enable it in your <span class="s1">php.ini</span>:

```
extension=redis
```

- - To verify it’s installed:

```
php -m | grep redis
```

## **Code**

Once all prerequisites are set up, create a new file named `<span class="s2">keydb.php</span>` and add the following code:

```php
<?php

$host = 'HOST';
$port = PORT;
$password = 'PASSWORD';

$keydb = new Redis();

try {
    $keydb->connect($host, $port);

    if (!$keydb->auth($password)) {
        throw new Exception('Authentication failed');
    }

    echo "Connected to KeyDB\n";

    $keydb->set("testKey", "Hello KeyDB");
    $value = $keydb->get("testKey");
    echo "Retrieved value: $value\n";

    $keydb->close();

} catch (Exception $e) {
    echo "KeyDB connection or operation failed: " . $e->getMessage() . "\n";
}
```

Open the terminal or command prompt and navigate to the directory where `<span class="s2">keydb.php</span>` is located. Once in the correct directory, run the script with the command:

```
php keydb.php
```

If the connection is successful, the terminal will display output similar to: