var_export

var_export(mixed $value, bool $return = false): ?string

Outputs a valid PHP code representation of a value (re-loadable via eval/include), often for config export.

C C23

C23
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"

int main(void) {
    cJSON *a = cJSON_CreateIntArray((int[]){1, 2, 3}, 3);

    cJSON *user = cJSON_CreateObject();
    cJSON_AddStringToObject(user, "name", "Alice");
    cJSON_AddNumberToObject(user, "age", 30);
    cJSON_AddItemToObject(user, "scores", cJSON_CreateIntArray((int[]){90, 85}, 2));

    char *s1 = cJSON_PrintUnformatted(a);
    char *s2 = cJSON_PrintUnformatted(user); // re-parsable JSON, the closest to var_export
    printf("%s\n%s\n", s1, s2);
    cJSON_free(s1); // use the paired deallocator provided by cJSON
    cJSON_free(s2);
    cJSON_Delete(a);
    cJSON_Delete(user);
    return 0;
}
Output
[1,2,3]
{"name":"Alice","age":30,"scores":[90,85]}

C++ C++26

C++26
#include <iostream>
#include "nlohmann/json.hpp"

int main() {
    // nlohmann json sorts keys by default; ordered_json keeps insertion order
    nlohmann::ordered_json a = {1, 2, 3};
    nlohmann::ordered_json user;
    user["name"] = "Alice";
    user["age"] = 30;
    user["scores"] = {90, 85};
    std::cout << a.dump() << "\n" << user.dump() << "\n"; // re-parsable JSON
    return 0;
}
Output
[1,2,3]
{"name":"Alice","age":30,"scores":[90,85]}

C# v11.0.100

v11.0.100
using System;
using System.Collections.Generic;
using System.Text.Json;

// no literal serializer; System.Text.Json emits re-parsable JSON
int[] a = { 1, 2, 3 };
var d = new Dictionary<string, object?> { ["name"] = "Alice", ["age"] = 30 };
var user = new User("Alice", new[] { 90, 85 });

Console.WriteLine(JsonSerializer.Serialize(a));
Console.WriteLine(JsonSerializer.Serialize(d));
Console.WriteLine(JsonSerializer.Serialize(user)); // records serialize as PascalCase properties

public record User(string Name, int[] Scores);
Output
[1,2,3]
{"name":"Alice","age":30}
{"Name":"Alice","Scores":[90,85]}

Go v1.27.4

v1.27.4
package main

import "fmt"

type User struct {
	Name   string
	Age    int
	Scores []int
}

func main() {
	a := []int{1, 2, 3}
	fmt.Printf("%#v\n", a) // %#v emits a valid Go literal

	m := map[string]any{"name": "Alice", "age": 30} // fmt sorts map keys, so the output is stable
	fmt.Printf("%#v\n", m)

	u := User{Name: "Alice", Age: 30, Scores: []int{90, 85}}
	fmt.Printf("%#v\n", u) // paste it straight back into Go code
}
Output
[]int{1, 2, 3}
map[string]interface {}{"age":30, "name":"Alice"}
main.User{Name:"Alice", Age:30, Scores:[]int{90, 85}}

Java v27.0.1

v27.0.1
import java.io.StringWriter;
import org.json.JSONWriter;

public class Main {
    public static void main(String[] args) {
        // org.json has no Java-literal serializer; JSONObject is HashMap-based (key order
        // not guaranteed), so JSONWriter serializes in write order as re-parsable JSON
        StringWriter sw = new StringWriter();
        JSONWriter w = new JSONWriter(sw);
        w.array().value(1).value(2).value(3).endArray();
        System.out.println(sw.toString());

        sw = new StringWriter();
        JSONWriter w2 = new JSONWriter(sw);
        w2.object()
          .key("name").value("Alice")
          .key("age").value(30)
          .key("scores").array().value(90).value(85).endArray()
          .endObject();
        System.out.println(sw.toString());
    }
}
Output
[1,2,3]
{"name":"Alice","age":30,"scores":[90,85]}

JavaScript v26.13.1

v26.13.1
const a = [1, 2, 3];
const user = { name: "Alice", age: 30, scores: [90, 85] };
console.log(JSON.stringify(a)); // valid JSON, not a JS literal (undefined/functions are lost)
console.log(JSON.stringify(user)); // string keys keep insertion order
Output
[1,2,3]
{"name":"Alice","age":30,"scores":[90,85]}

PHP v8.6.0

v8.6.0
var_export([1, 2, 3]);
echo "\n";

var_export(["name" => "Alice", "age" => 30]);
echo "\n";

$u = new stdClass();
$u->name = "Alice";
$u->scores = [90, 85];
var_export($u);
echo "\n";
Output
array (
  0 => 1,
  1 => 2,
  2 => 3,
)
array (
  'name' => 'Alice',
  'age' => 30,
)
(object) array(
   'name' => 'Alice',
   'scores' => 
  array (
    0 => 90,
    1 => 85,
  ),
)

Python v3.15.2

v3.15.2
a = [1, 2, 3]
user = {"name": "Alice", "age": 30, "scores": [90, 85]}
print(repr(a)) # repr output is a valid Python literal, eval-able (ast.literal_eval for safety)
print(repr(user))
Output
[1, 2, 3]
{'name': 'Alice', 'age': 30, 'scores': [90, 85]}

Rust v1.99.1

v1.99.1
fn main() {
    // Debug output is not valid Rust; serde_json emits re-parsable JSON instead
    let a = serde_json::json!([1, 2, 3]);
    let user = serde_json::json!({"name": "Alice", "age": 30, "scores": [90, 85]});
    println!("{}", a.to_string());
    println!("{}", user.to_string()); // preserve_order keeps insertion order
}
Output
[1,2,3]
{"name":"Alice","age":30,"scores":[90,85]}

TypeScript v26.13.1

v26.13.1
const a: number[] = [1, 2, 3];
const user: { name: string; age: number; scores: number[] } = {
  name: "Alice",
  age: 30,
  scores: [90, 85],
};
console.log(JSON.stringify(a)); // valid JSON, not a TS/JS literal
console.log(JSON.stringify(user)); // insert order preserved
Output
[1,2,3]
{"name":"Alice","age":30,"scores":[90,85]}

Language differences

C: no reflection — JSON via cJSON as well (insertion order kept).

C++: no reflection — JSON via nlohmann; use ordered_json to keep insertion order (plain json sorts keys).

C#: no literal serializer — the ecosystem answer is JSON (System.Text.Json); records serialize properties with their declared names (PascalCase).

Go: %#v output is a valid Go expression you can paste straight back into code — the closest any language gets to var_export's "executable literal" role.

Java: no literal serializer — the ecosystem answer is JSON (org.json); JSONObject is HashMap-based with no key-order guarantee, so use JSONWriter to serialize in write order for stable output.

JavaScript: JSON.stringify emits JSON, not a JS literal (loses undefined/functions/Symbols, and Map/Set are unsupported); object string keys keep insertion order.

PHP: var_export's uniqueness is that its output is "valid PHP code" — re-loadable directly via eval/include, the classic config-cache export move (second argument true returns the string); since PHP 7.3 stdClass exports as (object) array(...) (eval-able without any magic method), while custom classes emit ClassName::__set_state(array(...)) and the class must implement __set_state for the code to actually evaluate; the quirky 'a' => followed by a newline is historical behavior — generators shouldn't try to tidy it.

Python: the closest analog is repr() — its output is directly a valid Python literal (ast.literal_eval for safe round-trips), but only for literal-shaped data: a class instance's default repr is not an eval-able constructor expression.

Rust: Debug printing is not re-parsable (it's a debug format), and serde_json only yields JSON, not a Rust literal — Rust has no direct var_export counterpart.

TypeScript: same as JS — JSON.stringify emits JSON, not a TS/JS literal.