Codectionary / Developer documentation / PHP

PHP strict array membership

in_array searches values rather than keys. Its strict argument requires both type and value to match, avoiding implicit conversions in identifiers and allowlists.

Syntax

in_array($value, $allowed, true)

Examples

PHP strict array membership

Run as a PHP 8.0+ CLI script, for example php example.php. The multibyte example additionally requires mbstring.

<?php
$allowed = [10, 20, 30];
var_export(in_array('20', $allowed, true)); echo PHP_EOL;
var_export(in_array(20, $allowed, true)); echo PHP_EOL;
  1. Output: false, then true. The string 20 and integer 20 are different under strict comparison.

Best practices

  • Normalize and validate input intentionally before checking membership. Use array_key_exists when searching keys instead of values.

Runtime & versions

PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

PHP Manual

Specifications & further reading