PHP code example of kariricode / dotenv

1. Go to this page and download the library: Download kariricode/dotenv library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kariricode / dotenv example snippets




aririCode\Dotenv\DotenvFactory;
use function KaririCode\Dotenv\env;

$dotenv = DotenvFactory::create(__DIR__ . '/../.env');
$dotenv->load();

// Now you can use the env() function to access your environment variables
$appName = env('KARIRI_APP_NAME');
$debug = env('KARIRI_APP_DEBUG');
$jsonConfig = env('KARIRI_JSON_CONFIG');
$arrayConfig = env('KARIRI_ARRAY_CONFIG');

$stringVar = env('STRING_VAR'); // string: "Hello World"
$intVar = env('INT_VAR');       // integer: 42
$floatVar = env('FLOAT_VAR');   // float: 3.14
$boolVar = env('BOOL_VAR');     // boolean: true
$nullVar = env('NULL_VAR');     // null
$arrayVar = env('ARRAY_VAR');   // array: ["item1", "item2", "item3"]
$jsonVar = env('JSON_VAR');     // array: ["key" => "value", "nested" => ["subkey" => "subvalue"]]

use KaririCode\Dotenv\Type\Detector\AbstractTypeDetector;

class CustomDetector extends AbstractTypeDetector
{
    public const PRIORITY = 100;

    public function detect(mixed $value): ?string
    {
        // Your detection logic here
        // Return the detected type as a string, or null if not detected
    }
}

$dotenv->addTypeDetector(new CustomDetector());

use KaririCode\Dotenv\Contract\Type\TypeCaster;

class CustomCaster implements TypeCaster
{
    public function cast(mixed $value): mixed
    {
        // Your casting logic here
    }
}

$dotenv->addTypeCaster('custom_type', new CustomCaster());