strpos finds the first position where a substring appears.

search result A search can return a numeric position; position zero is still a real match.

String Search

term
string_search.php
Replay: real traced execution (multi-file project)
<?php
$term = "cat";
$text = "cat dog fox";
$position = strpos($text, $term);
$found = $position !== false;
$label = $found ? "found" : "missing";

echo "term=" . $term . "\n";
echo "position=" . $position . "\n";
echo "label=" . $label . "\n";
<?php
$term = "dog";
$text = "cat dog fox";
$position = strpos($text, $term);
$found = $position !== false;
$label = $found ? "found" : "missing";

echo "term=" . $term . "\n";
echo "position=" . $position . "\n";
echo "label=" . $label . "\n";
<?php
$term = "fox";
$text = "cat dog fox";
$position = strpos($text, $term);
$found = $position !== false;
$label = $found ? "found" : "missing";

echo "term=" . $term . "\n";
echo "position=" . $position . "\n";
echo "label=" . $label . "\n";
  1. $term ← cat, $text ← cat dog fox, $position ← 0, $found ← 1, $label ← found

    1<?php2$term→ cat = "cat"; //@term="dog", "fox"3$text→ cat dog fox = "cat dog fox";4$position→ 0 = strpos($textcat dog fox, $termcat);5$found→ 1 = $position0 !== false;6$label→ found = $found1 ? "found" : "missing";78echo "term=" . $termcat . "\n";9echo "position=" . $position0 . "\n";10echo "label=" . $labelfound . "\n";
    outputterm=cat
    position=0
    label=found
  1. $term ← dog, $text ← cat dog fox, $position ← 4, $found ← 1, $label ← found

    1<?php2$term→ dog = "dog";3$text→ cat dog fox = "cat dog fox";4$position→ 4 = strpos($textcat dog fox, $termdog);5$found→ 1 = $position4 !== false;6$label→ found = $found1 ? "found" : "missing";78echo "term=" . $termdog . "\n";9echo "position=" . $position4 . "\n";10echo "label=" . $labelfound . "\n";
    outputterm=dog
    position=4
    label=found
  1. $term ← fox, $text ← cat dog fox, $position ← 8, $found ← 1, $label ← found

    1<?php2$term→ fox = "fox";3$text→ cat dog fox = "cat dog fox";4$position→ 8 = strpos($textcat dog fox, $termfox);5$found→ 1 = $position8 !== false;6$label→ found = $found1 ? "found" : "missing";78echo "term=" . $termfox . "\n";9echo "position=" . $position8 . "\n";10echo "label=" . $labelfound . "\n";
    outputterm=fox
    position=8
    label=found