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 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
array(1) { ["'a' IN (?, ?, ?)"]=> int(1) } array(1) { ["'a' IN (:letters_0, :letters_1, :letters_2)"]=> int(1) }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 array(1) { ["'a' IN (?, ?, ?)"]=> int(1) } array(1) { ["'a' IN (:letters_0, :letters_1, :letters_2)"]=> int(1) }
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
array(1) { ["'a' IN (?, ?, ?)"]=> string(1) "1" } array(1) { ["'a' IN (:letters_0, :letters_1, :letters_2)"]=> string(1) "1" }
Output for 7.0.0 - 7.0.33
Parse error: syntax error, unexpected '=' in /in/QmVRI on line 10
Process exited with code 255.
Output for 5.6.0 - 5.6.40
Warning: Unsupported declare 'strict_types' in /in/QmVRI on line 2 Parse error: syntax error, unexpected '=' in /in/QmVRI on line 10
Process exited with code 255.

preferences:
242.71 ms | 401 KiB | 288 Q