3v4l.org

run code in 300+ PHP versions simultaneously
<?php function sql(callable $expr): array { $placeholder = new class { public function __toString(): string { return '?'; } }; $expr = Closure::fromCallable($expr); $expr = Closure::bind($expr, $placeholder); $params = []; $generator = $expr(); while ($generator->valid()) { $params[] = $generator->current(); // Get the value from "yield" $generator->send('this'); // Insert placeholder } return [$generator->getReturn(), $params]; } [$query, $params] = sql(fn() => "SELECT * FROM users WHERE id = ${yield 42} OR id = ${yield 0xDEADBEEF}"); var_dump( $query, $params, );
Output for git.master, git.master_jit, rfc.property-hooks
Deprecated: Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead in /in/8lPf3 on line 24 Deprecated: Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead in /in/8lPf3 on line 24 string(42) "SELECT * FROM users WHERE id = ? OR id = ?" array(2) { [0]=> int(42) [1]=> int(3735928559) }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
116.1 ms | 406 KiB | 5 Q