# How to Change the Case of Generated License Keys in PHP: Uppercase vs Lowercase

> Easily control license key case in PHP. Learn how to generate uppercase or lowercase keys using the SunLicense constructor in this quick guide.

- Repository: [Mehmet Selcuk Batal/php-license-key-generator](https://github.com/msbatal/php-license-key-generator)
- Tags: how-to-guide
- Published: 2026-03-07

---

**Pass `'upper'` or `'lower'` as the third argument to the `SunLicense` constructor** to control whether generated license keys contain uppercase or lowercase letters.

When generating license keys with the `msbatal/php-license-key-generator` library, you can change the case of generated license keys to produce either **uppercase** or **lowercase** output. The `SunLicense` class provides a dedicated `$case` property that controls this behavior during instantiation or at runtime.

## How the Case Parameter Works in SunLicense.php

The `SunLicense` class in [`SunLicense.php`](https://github.com/msbatal/php-license-key-generator/blob/main/SunLicense.php) exposes a `$case` property that determines the character set used during key generation. According to the source code, this property defaults to `'upper'` and accepts either `'upper'` or `'lower'` as valid values.

### Constructor Signature and Default Behavior

When instantiating the `SunLicense` class, the third parameter controls letter casing:

```php
public function __construct($prefix = null, $template = null, $case = 'upper', $count = null)

```

Lines 66-67 of [`SunLicense.php`](https://github.com/msbatal/php-license-key-generator/blob/main/SunLicense.php) show the assignment logic:

```php
if (!empty($case)) {
    $this->case = $case;               // case of letters (upper/lower)
}

```

The internal `$case` property defaults to `'upper'` as declared on line 37.

### Key Generation Logic

Inside the `generate()` method (line 120), the class uses conditional ASCII character generation based on the `$case` value:

```php
if ($this->case == 'lower') {
    $key .= chr(rand(97, 122)); // generate lowercase letters   [lines 86-87]
} else {
    $key .= chr(rand(65, 90)); // generate uppercase letters   [lines 88-89]
}

```

This logic uses `chr(rand(97, 122))` for lowercase a-z and `chr(rand(65, 90))` for uppercase A-Z.

## Setting License Key Case During Instantiation

### Generate Uppercase Keys (Default)

To create license keys with uppercase letters, pass `'upper'` as the third argument or omit it entirely to use the default:

```php
<?php
require_once 'SunLicense.php';

$license = new SunLicense('SLK', 'AA99-9A9A-A9A9-99AA', 'upper', 3);
print_r($license->generate());
// Example output: ['SLK-AB12-CD34-EF56', 'SLK-GH78-IJ90-KL12', 'SLK-MN34-OP56-QR78']
?>

```

### Generate Lowercase Keys

For lowercase output, explicitly pass `'lower'` as implemented in `msbatal/php-license-key-generator`:

```php
<?php
require_once 'SunLicense.php';

$license = new SunLicense('SLK', 'aa99-9a9a-a9a9-99aa', 'lower', 2);
print_r($license->generate());
// Example output: ['SLK-ab12-cd34-ef56', 'SLK-gh78-ij90-kl12']
?>

```

## Changing Case After Object Creation

You can also change the case behavior by directly modifying the public `$case` property after instantiation. This approach is useful when you need to generate multiple batches with different casing using the same template configuration:

```php
<?php
require_once 'SunLicense.php';

$license = new SunLicense('SLK', null, null, 1);   // defaults to upper
$license->case = 'lower';                         // manually override
echo $license->generate()[0];                     // e.g., SLK-ab12-cd34-ef56
?>

```

The [`test/SunLicense.php`](https://github.com/msbatal/php-license-key-generator/blob/main/test/SunLicense.php) file in the repository contains additional usage examples demonstrating case configuration and validation.

## Summary

- **Constructor parameter**: Pass `'upper'` or `'lower'` as the third argument to `SunLicense` to set the case of generated license keys.
- **Default behavior**: The library defaults to `'upper'` case as defined in [`SunLicense.php`](https://github.com/msbatal/php-license-key-generator/blob/main/SunLicense.php) at line 37.
- **ASCII generation**: Uppercase uses `chr(rand(65, 90))` (lines 88-89); lowercase uses `chr(rand(97, 122))` (lines 86-87).
- **Runtime modification**: The `$case` property remains publicly accessible, allowing case changes after object instantiation without creating a new instance.

## Frequently Asked Questions

### What is the default case for generated license keys?

The `SunLicense` class defaults to `'upper'` case. This is defined in the property declaration on line 37 of [`SunLicense.php`](https://github.com/msbatal/php-license-key-generator/blob/main/SunLicense.php). If you omit the third constructor parameter or pass `null`, the generator will produce license keys containing uppercase letters A-Z.

### Can I change the case after creating the SunLicense object?

Yes. The `$case` property is public, so you can modify it directly after instantiation. Set `$license->case = 'lower'` or `$license->case = 'upper'` before calling the `generate()` method to switch the output format without creating a new object.

### Does the case setting affect the prefix or template characters?

No. The `$case` setting only affects randomly generated letter characters within the key segments. Literal characters defined in your prefix or template parameters remain exactly as specified. For example, if your template contains literal 'A' characters, they stay as 'A' regardless of the case setting.

### What happens if I pass an invalid case value?

Any value other than `'lower'` will result in uppercase letter generation. The `generate()` method checks `if ($this->case == 'lower')` (line 86); if this condition fails, the else clause (lines 88-89) executes, producing uppercase characters. Therefore, passing `'upper'`, `'mixed'`, or other strings defaults to uppercase behavior.