You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](https://packagist.org/packages/devcoder-xyz/php-dotenv)
5
5
6
+
## Introduction
7
+
PHP-DotEnv is a lightweight PHP library designed to simplify the management of environment variables in your PHP applications. It provides an elegant solution for loading configuration values from a `.env` file into the environment variables accessible via `getenv()`, `$_ENV`, and `$_SERVER`. This documentation aims to guide you through the installation, usage, and features of PHP-DotEnv.
8
+
6
9
## Installation
7
10
8
-
Use [Composer](https://getcomposer.org/)
11
+
To install PHP-DotEnv, you can use [Composer](https://getcomposer.org/), the dependency manager for PHP.
9
12
10
13
### Composer Require
11
-
```
14
+
```bash
12
15
composer require devcoder-xyz/php-dotenv
13
16
```
14
17
15
18
## Requirements
16
19
17
-
* PHP version 7.4
20
+
- PHP version 7.4 or higher
18
21
19
-
**How to use ?**
22
+
## Usage
20
23
21
-
```
24
+
### 1. Define Environment Variables
25
+
26
+
Before using PHP-DotEnv, you need to define your environment variables in a `.env` file. This file should be placed in the root directory of your project. Each line in the file should follow the `KEY=VALUE` format.
27
+
28
+
```dotenv
22
29
APP_ENV=dev
23
30
DATABASE_DNS=mysql:host=localhost;dbname=test;
24
31
DATABASE_USER="root"
@@ -28,7 +35,9 @@ NUMBER_LITERAL=0
28
35
NULL_VALUE=null
29
36
```
30
37
31
-
## Load the variables
38
+
### 2. Load the Variables
39
+
40
+
After defining your environment variables, you can load them into your PHP application using PHP-DotEnv.
Once loaded, you can access the environment variables using PHP's `getenv()` function.
54
+
43
55
```php
44
56
/**
45
-
* string(33) "mysql:host=localhost;dbname=test;"
57
+
* Retrieve the value of DATABASE_DNS
46
58
*/
47
59
var_dump(getenv('DATABASE_DNS'));
60
+
```
48
61
49
-
/**
50
-
* Removes double and single quotes from the variable:
51
-
*
52
-
* string(4) "root"
53
-
*/
54
-
var_dump(getenv('DATABASE_USER'));
62
+
### Automatic Type Conversion
55
63
56
-
/**
57
-
* Processes booleans as such:
58
-
*
59
-
* bool(true)
60
-
*/
61
-
var_dump(getenv('MODULE_ENABLED'));
64
+
PHP-DotEnv provides automatic type conversion for certain types of values:
62
65
63
-
/**
64
-
* Process the numeric value:
65
-
*
66
-
* int(0)
67
-
*/
68
-
var_dump(getenv('NUMBER_LITERAL'));
66
+
- Booleans: Processed as `true` or `false`.
67
+
- Quoted Strings: Surrounding quotes are removed.
68
+
- Null Values: Converted to `null`.
69
+
- Numeric Values: Converted to integers or floats.
69
70
70
-
/**
71
-
* Check for literal null values:
72
-
*
73
-
* NULL
74
-
*/
75
-
var_dump(getenv('NULL_VALUE'));
76
-
```
71
+
## Processors
72
+
73
+
PHP-DotEnv allows you to define custom processors to handle specific types of values in your `.env` file. These processors enable you to control how values are parsed and converted.
77
74
78
-
Ideal for small project
75
+
### BooleanProcessor
79
76
80
-
Simple and easy!
77
+
The `BooleanProcessor` converts boolean values specified in the `.env` file to PHP boolean types (`true` or `false`).
81
78
82
-
# Processors
79
+
```dotenv
80
+
MODULE_ENABLED=true
81
+
```
83
82
84
-
Also the variables are parsed according to the configuration passed as parameter to the constructor. The available processors are:
83
+
### QuotedProcessor
85
84
86
-
## BooleanProcessor
85
+
The `QuotedProcessor` removes surrounding quotes from quoted strings in the `.env` file.
87
86
88
-
``VARIABLE=false`` will be processed to ```bool(false)```
87
+
```dotenv
88
+
DATABASE_USER="root"
89
+
```
89
90
90
-
NOTE: ``VARIABLE="true"`` will be processed to ```string(4) "true"```
91
+
### NullProcessor
91
92
92
-
## QuotedProcessor
93
+
The `NullProcessor` converts the string "null" to the PHP `null` value.
93
94
94
-
``VARIABLE="anything"`` will be processed to ```string(8) "anything"```
95
+
```dotenv
96
+
NULL_VALUE=null
97
+
```
95
98
96
-
##NullProcessor
99
+
### NumberProcessor
97
100
98
-
``VARIABLE=null`` will be processed to ```NULL```
101
+
The `NumberProcessor` converts numeric values to integers or floats.
99
102
100
-
## NumberProcessor
103
+
```dotenv
104
+
NUMBER_LITERAL=0
105
+
```
101
106
102
-
``VARIABLE=0`` will be processed to ```int(0)```
107
+
## Conclusion
103
108
104
-
``VARIABLE=0.1`` will be processed to ```float(0.1)```
109
+
PHP-DotEnv offers a straightforward and efficient solution for managing environment variables in PHP applications. By providing automatic type conversion and customizable processors, it simplifies the process of loading and handling configuration values from `.env` files. Whether you're working on a small project or a large-scale application, PHP-DotEnv can help streamline your development process and ensure smooth configuration management. Explore its features, integrate it into your projects, and experience the convenience it brings to your PHP development workflow.
0 commit comments