3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types = 1); namespace at\PDOi; use PDO as BasePDO, PDOStatement; $pdo = new PDO('sqlite::memory:'); [$markers, $values] = $pdo->arrayParam(['a', 'b', 'c']); $stmt = $pdo->prepare("select 'a' IN ({$markers})"); $stmt->execute($values); var_dump($stmt->fetch(PDO::FETCH_ASSOC)); [$markers, $values] = $pdo->arrayParam(['a', 'b', 'c'], 'letters'); $stmt = $pdo->prepare("select 'a' IN ({$markers})"); $stmt->execute($values); var_dump($stmt->fetch(PDO::FETCH_ASSOC)); /** * @package at.pdoi * @author Adrian <adrian@enspi.red> * @copyright 2014 - 2017 * @license GPL-3.0 (only) * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License, version 3. * The right to apply the terms of later versions of the GPL is RESERVED. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with this program. * If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>. */ /** * minor changes and additions to enhance PDO's security and convenience. */ class PDO extends BasePDO { /** * {@inheritDoc} * @see http://php.net/PDO.__construct * * adds good default options. */ public function __construct($dsn, $username = null, $password = null, $options = []) { $options += [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false ]; parent::__construct($dsn, $username, $password, $options); } /** * expands arrays into multiple named or positional parameters. * * @param array $values the values to parameterize * @param string $name name for named parameters (omit for positional parameters) * @return array { * @type string $0 comma-separated parameter markers (sql fragment) * @type array $1 parameter values, as a map or ordered list * } */ public function arrayParam(array $values, string $name = null) { $values = array_values($values); $i = 0; $keys = array_map( function ($value) use ($name, &$i) { $marker = $name ? ":{$name}_{$i}" : '?'; $i++; return $marker; }, $values ); if ($name) { $values = array_combine($keys, $values); } return [implode(', ', $keys), $values]; } /** * prepares and executes a statement in one step. * * @param string $sql the sql statement to prepare * @param array $params parameter values to execute against the statement * @return PDOStatement the executed statement object on success */ public function preparedQuery(string $sql, array $params = []) : PDOStatement { $stmt = $this->prepare($sql); $stmt->execute($params); return $stmt; } }
Output for git.master, git.master_jit, rfc.property-hooks
array(1) { ["'a' IN (?, ?, ?)"]=> int(1) } array(1) { ["'a' IN (:letters_0, :letters_1, :letters_2)"]=> int(1) }

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:
32.93 ms | 401 KiB | 8 Q