Codectionary / Developer documentation / PHP

PHP substring predicates

PHP 8 str_contains returns a boolean for a case-sensitive substring search. It avoids confusing a match at position zero with failure.

Syntax

str_contains($text, $needle)

Examples

PHP substring predicates

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

<?php
$filename = 'report-final.csv';
var_export(str_contains($filename, 'report')); echo PHP_EOL;
var_export(str_contains($filename, 'REPORT')); echo PHP_EOL;
  1. Output: true, then false. A match at the beginning succeeds; changing letter case changes the result.

Best practices

  • Remember that an empty needle always matches. Decide whether case-sensitive byte matching is appropriate for the input.

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