PHPackages                             selfiens/var-export - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. selfiens/var-export

ActiveLibrary

selfiens/var-export
===================

PHP value serializer

v1.0.0(1y ago)01MITPHPPHP &gt;=8.1

Since May 16Pushed 1y ago1 watchersCompare

[ Source](https://github.com/selfiens/php-var-export)[ Packagist](https://packagist.org/packages/selfiens/var-export)[ RSS](/packages/selfiens-var-export/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

PHP VarExport is a lightweight PHP library for serializing variables into a string representation of PHP code. It produces output that mimics modern PHP syntax, particularly short array notation (`[]`). This makes it a useful alternative to PHP's native `var_export()` or `json_encode()` when you need human-readable PHP code that can be directly embedded or evaluated.

Key Features
------------

[](#key-features)

- **Modern PHP Syntax:** Outputs arrays using short array syntax (`[]`).
- **Two Output Styles:**
    - `compact()`: Generates a concise, single-line representation.
    - `formatted()`: Produces a human-readable, indented representation.
- **Comprehensive Type Handling:** Supports various data types including:
    - Arrays (both list and associative)
    - Scalars (strings, integers, floats, booleans, null)
    - Objects (converts to an array via a public `toArray()` method or public properties)
    - Resources (represented as a descriptive string)
    - Callables (including Closures, represented as descriptive strings)
- **Recursion Depth Control:** Prevents infinite loops and excessive memory usage with a configurable maximum depth.
- **Precise Float Formatting:** Custom float-to-string conversion (`floatToString()`) aims to avoid scientific E-notation and ensures whole floats are represented with `.0` (e.g., `1.0` instead of `1`).
- **Informative Comments:** Includes comments for object class names and callables (in formatted mode) for better readability.

Requirements
------------

[](#requirements)

- PHP 8.1 or higher

Installation
------------

[](#installation)

Install the library using [Composer](https://getcomposer.org/):

```
composer require selfiens/var-export
```

Usage
-----

[](#usage)

Import the class and use its static methods:

```
