PHP code example of bermudaphp / var-export

1. Go to this page and download the library: Download bermudaphp/var-export 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/ */

    

bermudaphp / var-export example snippets


use Bermuda\VarExport\VarExporter;

// Simple array export
$array = ['foo' => 'bar', 'nested' => [1, 2, 3]];
echo VarExporter::export($array);
// Output: ['foo' => 'bar', 'nested' => [1, 2, 3]]

// Pretty formatting
echo VarExporter::exportPretty($array);
// Output:
// [
//     'foo' => 'bar',
//     'nested' => [
//         1,
//         2,
//         3
//     ]
// ]

use Bermuda\VarExport\VarExporter;

// Scalar values
echo VarExporter::export(42);           // 42
echo VarExporter::export('hello');      // 'hello'
echo VarExporter::export(true);         // true
echo VarExporter::export(null);         // null

// Special float values
echo VarExporter::export(INF);          // INF
echo VarExporter::export(-INF);         // -INF
echo VarExporter::export(NAN);          // NAN

use Bermuda\VarExport\VarExporter;

$data = [
    'users' => [
        ['name' => 'John', 'age' => 30],
        ['name' => 'Jane', 'age' => 25]
    ],
    'config' => [
        'debug' => true,
        'timeout' => 30
    ]
];

// Standard formatting (single line)
echo VarExporter::export($data);

// Pretty formatting (multi-line with indentation)
echo VarExporter::exportPretty($data);

use Bermuda\VarExport\VarExporter;

$multiplier = 2;
$closure = function($x) use ($multiplier) {
    return $x * $multiplier;
};

// Export closure with full source code
echo VarExporter::export($closure);
// Output: function($x) use ($multiplier) { return $x * $multiplier; }

class A {
    public function call()
    {
        $closure = fn(): string => self::class ;
        dd(VarExporter::export($closure));
    }
}

^ "fn(): string => \A::class"

use Bermuda\VarExport\{VarExporter, FormatterConfig, FormatterMode};

$config = new FormatterConfig(
    mode: FormatterMode::PRETTY,
    indent: '  ',                    // 2 spaces instead of 4
    maxDepth: 50,                   // Maximum nesting depth
    sortKeys: true,                 // Sort array keys
    trailingComma: true            // Add trailing comma in arrays
);

$array = ['c' => 3, 'a' => 1, 'b' => 2];
echo VarExporter::export($array, $config);

use function Bermuda\VarExport\{export_var, export_array, export_closure};

// Export any variable with pretty formatting by default
echo export_var(['foo' => 'bar']);

// Export array specifically
echo export_array([1, 2, 3]);

// Export closure specifically
$fn = fn($x) => $x * 2;
echo export_closure($fn);

use Bermuda\VarExport\{VarExporter, FormatterConfig, FormatterMode};

// Create custom configuration
$config = new FormatterConfig(
    mode: FormatterMode::PRETTY,
    indent: "\t",                   // Use tabs
    sortKeys: true,                 // Sort keys alphabetically
    trailingComma: true            // Add trailing commas
);

// Apply to specific export
$result = VarExporter::export($data, $config);

// Set as default for all exports
VarExporter::setDefaultConfig($config);

use Bermuda\VarExport\{VarExporter, ArrayExporter, ClosureExporter};

// Register custom array exporter
$customArrayExporter = new ArrayExporter($customConfig);
VarExporter::setExporter($customArrayExporter);

// Register custom closure exporter
$customClosureExporter = new ClosureExporter($customConfig);
VarExporter::setExporter($customClosureExporter);

use Bermuda\VarExport\{VarExporter, ExportException};

try {
    $object = new stdClass();
    VarExporter::export($object);
} catch (ExportException $e) {
    echo "Export failed: " . $e->getMessage();
    // Access the problematic variable
    $problematicVar = $e->var;
}