# Core\_helper

Class Core\_helper in PHPFast provides several **utility functions** to manage helpers, system configurations, server information, and environment variables in a PHP application.

Functions Helper in Core

## `load_helpers()`

Loads helper files dynamically from either the **system** or **application** directories.

```php
function load_helpers(array $helpers = []) {
```

How It Works:

* Uses a global variable `$fast_helpers` to track loaded helpers.
* Checks if the helper is **already loaded** to avoid redundant imports.
* Searches for the helper file in:
  * `ROOT_PATH/system/Helpers/`
  * `ROOT_PATH/application/Helpers/`
* If the file exists, it is **included (`require_once`)**, otherwise, it throws an **exception**.

Example:

```php
load_helpers(['form', 'url']); // Loads form_helper.php and url_helper.php
```

## `DateTime()`

Returns the **current date and time** in the `YYYY-MM-DD HH:MM:SS` format.

```php
function DateTime() {
    return date('Y-m-d H:i:s');
}
```

Example:

```php
echo DateTime(); 
// Output: 2025-03-01 12:34:56
```

## `version_php()`

Returns the **current PHP version**.

Example:

```php
echo version_php(); 
// Output: 8.3.16
```

## `dir_writable()`

* Checks if a given **directory exists and is writable**.
* Returns `true` if writable, otherwise `false`.

Example:

```php
echo dir_writable('/writable/uploads') ? "Writable" : "Not Writable";
```

## `server_info()`

* Returns **an array of server information**, including:
  * PHP version
  * Web server software
  * Document root
  * Server name
  * Server protocol

Example:

```php
[
    [php_version] => 8.3.16
    [server_software] => nginx/1.27.3
    [document_root] => C:/laragon/www/cms/public
    [server_name] => cms.test
    [server_protocol] => HTTP/1.1
]
```

## `random_string()`

Generates a **random alphanumeric string** of the specified length.

```php
function random_string($length = 10) {}
```

Example:

```php
echo random_string(12); 
// Output: A1b2C3d4E5F6
```

## `config()`

Loads **configuration values** from a specified file.

```php
function config($key = '', $file = 'Config') {}
```

* Reads the `/application/Config/Config.php` file and returns the requested setting.
* Uses a static `$config` array to **cache values for performance**.

Example:

```php
$siteName = config('site_name'); // Loads the site_name from Config.php.
```

## `option()`

* Retrieves an **option value** from the `Options.php` config file or from the **database**.
* If the key is not found in the file, it **queries the database** for the value.

```php
function option($key, $lang = LANG) {}
```

Example:

```php
$siteTitle = option('site_title');
// Gets the site title from the options table or configuration file.
```

## `option_set()`

* Updates an **option value** in the configuration file (`Options.php`).
* Supports **multi-language values** if `$lang` is specified.

```php
function option_set($key, $value, $lang = '') {}
```

* Loads the `Options.php` file.
* Updates the key-value pair.
* Saves the new values back to the file.

Example:

```php
option_set('site_title', 'PHPFast site');
// Updates the site_title value in Options.php.

echo option('site_title');
// Ouput: PHPFast site
```

## `env()`

Retrieves **environment variables** from the system or `.env` file.

```php
function env($key, $default = null) {}
```

* First, checks if the value **exists in cache** (`$env_cache`).
* If not found, retrieves it using `getenv()`.
* Cleans the value to prevent security issues (`htmlspecialchars`).
* Converts **"true", "false", "null"** strings into actual boolean values.

Example:

```php
$dbHost = env('DB_HOST', 'localhost');
// Retrieves the database host from environment variables.
```

## `_bytes()`

Converts a **storage unit string** (e.g., `2G`, `512M`, `128K`) into **bytes**.

```php
function _bytes($size) {}
```

* Extracts the **unit** (G, M, K).
* Converts it into bytes accordingly.

Example:

```php
echo _bytes('2G'); 
// Output: 2147483648
```

<table data-header-hidden><thead><tr><th width="242"></th><th></th></tr></thead><tbody><tr><td><strong>Function</strong></td><td><strong>Purpose</strong></td></tr><tr><td><code>load_helpers()</code></td><td>Load helper files dynamically.</td></tr><tr><td><code>DateTime()</code></td><td>Get the current date and time.</td></tr><tr><td><code>version_php()</code></td><td>Retrieve the PHP version.</td></tr><tr><td><code>dir_writable()</code></td><td>Check if a directory is writable.</td></tr><tr><td><code>server_info()</code></td><td>Get server details (PHP version, web server, etc.).</td></tr><tr><td><code>random_string()</code></td><td>Generate a random alphanumeric string.</td></tr><tr><td><code>config()</code></td><td>Retrieve system configuration settings.</td></tr><tr><td><code>option()</code></td><td>Fetch system options from a file or database.</td></tr><tr><td><code>option_set()</code></td><td>Update an option value in <code>Options.php</code>.</td></tr><tr><td><code>env()</code></td><td>Retrieve environment variables from <code>.env</code> or system.</td></tr><tr><td><code>_bytes()</code></td><td>Convert storage units (G, M, K) to bytes.</td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cmsfullform.com/documents/helpers/core_helper.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
