print_r
print_r(mixed $value, bool $return = false): string|true
Prints a human-readable representation of a value (arrays/objects as indented trees), without type info.
C C23
#include <stdio.h>
/* no reflection: hand-written printing is the C idiom */
struct User {
const char *name;
int age;
int scores[2];
};
int main(void) {
int a[3] = {1, 2, 3};
printf("[%d, %d, %d]\n", a[0], a[1], a[2]);
struct User u = {"Alice", 30, {90, 85}};
printf("{name: %s, age: %d, scores: [%d, %d]}\n",
u.name, u.age, u.scores[0], u.scores[1]);
return 0;
}
[1, 2, 3]
{name: Alice, age: 30, scores: [90, 85]}
C++ C++26
#include <iostream>
#include <string>
#include <vector>
struct User {
std::string name;
int age;
std::vector<int> scores;
};
// no reflection: provide an operator<< overload for readable dumping
std::ostream &operator<<(std::ostream &os, const User &u) {
os << "{name: " << u.name << ", age: " << u.age << ", scores: [";
for (size_t i = 0; i < u.scores.size(); ++i) {
if (i > 0) os << ", ";
os << u.scores[i];
}
os << "]}";
return os;
}
int main() {
std::vector<int> a{1, 2, 3};
std::cout << "[";
for (size_t i = 0; i < a.size(); ++i) {
if (i > 0) std::cout << ", ";
std::cout << a[i];
}
std::cout << "]\n";
User u{"Alice", 30, {90, 85}};
std::cout << u << "\n";
return 0;
}
[1, 2, 3]
{name: Alice, age: 30, scores: [90, 85]}
C# v11.0.100
using System;
using System.Collections.Generic;
using System.Text.Json;
// no readable-dump builtin; serialize to JSON as the quick inspect idiom
int[] a = { 1, 2, 3 };
Console.WriteLine(JsonSerializer.Serialize(a));
var d = new Dictionary<string, object> { ["name"] = "Alice", ["age"] = 30 };
Console.WriteLine(JsonSerializer.Serialize(d));
var user = new User("Alice", new[] { 90, 85 });
Console.WriteLine(JsonSerializer.Serialize(user)); // properties serialize with their declared names
public record User(string Name, int[] Scores);
[1,2,3]
{"name":"Alice","age":30}
{"Name":"Alice","Scores":[90,85]}
Go v1.27.4
package main
import "fmt"
type User struct {
Name string
Age int
Scores []int
}
func main() {
a := []int{1, 2, 3}
fmt.Println(a) // %v is the default
m := map[string]any{"name": "Alice", "age": 30}
fmt.Println(m) // map keys print in sorted order
u := User{Name: "Alice", Age: 30, Scores: []int{90, 85}}
fmt.Println(u) // %v: values only
fmt.Printf("%+v\n", u) // %+v: field names added
}
[1 2 3]
map[age:30 name:Alice]
{Alice 30 [90 85]}
{Name:Alice Age:30 Scores:[90 85]}
Java v27.0.1
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Main {
record User(String name, int age, List<Integer> scores) {}
public static void main(String[] args) {
int[] a = {1, 2, 3};
System.out.println(Arrays.toString(a)); // plain toString prints [I@1b6d3586-style hash
Map<String, Object> d = new LinkedHashMap<>();
d.put("name", "Alice");
d.put("age", 30);
System.out.println(d); // Map toString is the quick readable dump
User user = new User("Alice", 30, List.of(90, 85));
System.out.println(user); // record auto-generates a field-expanding toString
}
}
[1, 2, 3]
{name=Alice, age=30}
User[name=Alice, age=30, scores=[90, 85]]
JavaScript v26.13.1
const a = [1, 2, 3];
console.log(a); // Node prints util.inspect format
const d = { name: "Alice", age: 30 };
console.log(d); // objects print expanded in Node; browsers fold, click to expand
const user = { name: "Alice", scores: [90, 85] };
console.log(user);
console.log(JSON.stringify(user)); // compact, log-file friendly
[ 1, 2, 3 ]
{ name: 'Alice', age: 30 }
{ name: 'Alice', scores: [ 90, 85 ] }
{"name":"Alice","scores":[90,85]}
PHP v8.6.0
print_r([1, 2, 3]);
print_r(["name" => "Alice", "age" => 30]);
$u = new stdClass();
$u->name = "Alice";
$u->scores = [90, 85];
print_r($u);
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Array
(
[name] => Alice
[age] => 30
)
stdClass Object
(
[name] => Alice
[scores] => Array
(
[0] => 90
[1] => 85
)
)
Python v3.15.2
from dataclasses import dataclass
from pprint import pprint
pprint([1, 2, 3])
pprint({"name": "Alice", "age": 30}) # pprint sorts dict keys by default
@dataclass
class User:
name: str
scores: list
pprint(vars(User("Alice", [90, 85]))) # vars() hands the instance __dict__ to pprint
[1, 2, 3]
{'age': 30, 'name': 'Alice'}
{'name': 'Alice', 'scores': [90, 85]}
Rust v1.99.1
use std::collections::BTreeMap;
#[derive(Debug)]
struct User {
name: String,
age: i32,
scores: Vec<i32>,
}
fn main() {
println!("{:#?}", vec![1, 2, 3]);
println!("{:#?}", BTreeMap::from([("age", "30"), ("name", "Alice")])); // BTreeMap sorts keys; HashMap order is unspecified
let u = User { name: "Alice".into(), age: 30, scores: vec![90, 85] };
println!("{:#?}", u); // pretty Debug, the Rust answer to print_r
}
[
1,
2,
3,
]
{
"age": "30",
"name": "Alice",
}
User {
name: "Alice",
age: 30,
scores: [
90,
85,
],
}
TypeScript v26.13.1
const a: number[] = [1, 2, 3];
console.log(a); // Node prints util.inspect format
interface Dict {
name: string;
age: number;
}
const d: Dict = { name: "Alice", age: 30 };
console.log(d);
interface User {
name: string;
scores: number[];
}
const user: User = { name: "Alice", scores: [90, 85] };
console.log(user);
console.log(JSON.stringify(user)); // compact, log-file friendly
[ 1, 2, 3 ]
{ name: 'Alice', age: 30 }
{ name: 'Alice', scores: [ 90, 85 ] }
{"name":"Alice","scores":[90,85]}
Language differences
C: no reflection — hand-written printf stitching a readable format field by field.
C++: no reflection — typically overloads operator<< for the type (write once, reuse everywhere); containers and structs all print as a readable tree.
C#: no built-in readable dump — the idiom is JsonSerializer.Serialize for a quick look; note records serialize properties with their declared names (PascalCase), and Dictionary key order is insertion order.
Go: %v/%+v print the readable form (%+v adds field names), maps print in sorted key order — Go's answer to the "tree for humans".
Java: collections/Map toString work out of the box; arrays need Arrays.toString; records auto-generate a field-expanding toString, plain classes need their own.
JavaScript: console.log is util.inspect format in Node (browsers fold by default, click to expand), while JSON.stringify is the compact log-file form (drops undefined/functions, throws on cycles).
PHP: print_r is the "tree for humans" — indexed arrays print as an indented [key] => value tree and objects as stdClass Object, but with no types or lengths (types via var_dump, re-parsable output via var_export); the second argument true returns the string instead of printing, handy for logging.
Python: readable-print idioms differ sharply across languages — stdlib pprint.pprint (wraps long lines automatically, sorts dict keys by default; pprint.pformat returns a string), and vars(obj) grabs an instance's __dict__ for inspection.
Rust: the official answer is #[derive(Debug)] + {:#?} pretty-printing, which indents nested structures into a tree automatically.
TypeScript: same as JS — console.log / JSON.stringify.