0
0
PHPprogramming~10 mins

String search functions (strpos, strstr) in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String search functions (strpos, strstr)
Start with string and search term
Call strpos or strstr
Check if search term found?
NoReturn false
Yes
Return position (strpos) or substring (strstr)
Use result
The program starts with a string and a search term, calls strpos or strstr, checks if the term is found, then returns the position or substring accordingly.
Execution Sample
PHP
<?php
$text = "hello world";
$pos = strpos($text, "world");
$sub = strstr($text, "world");
echo $pos . " " . $sub;
?>
This code searches for "world" in "hello world" using strpos and strstr, then prints the position and substring.
Execution Table
StepFunction CallInput StringSearch TermResultExplanation
1strpos($text, "world")"hello world""world"6"world" starts at index 6 in "hello world"
2strstr($text, "world")"hello world""world""world"Returns substring from "world" to end
3echo $pos . " " . $subN/AN/A6 worldPrints position and substring
💡 All steps complete; strpos found "world" at index 6; strstr returned substring "world"
Variable Tracker
VariableStartAfter strposAfter strstrFinal
$text"hello world""hello world""hello world""hello world"
$posundefined666
$subundefinedundefined"world""world"
Key Moments - 3 Insights
Why does strpos return 6 instead of 7 for the position?
Because strpos counts from zero, so the first character 'h' is index 0, making 'w' in 'world' at index 6 (see execution_table step 1).
What happens if the search term is not found?
strpos returns false and strstr returns false, so you should check the result before using it (not shown in this example).
Why does strstr return only 'world' and not the whole string?
strstr returns the part of the string starting from the search term to the end (see execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $pos after step 1?
A6
B5
Cfalse
D7
💡 Hint
Check the 'Result' column in execution_table row for step 1.
At which step does strstr return the substring starting from the search term?
AStep 1
BStep 3
CStep 2
DNo step returns substring
💡 Hint
Look at the 'Function Call' and 'Result' columns in execution_table.
If the search term was not found, what would strpos return?
A0
Bfalse
Cempty string
D-1
💡 Hint
Recall that strpos returns false when the term is missing (see key_moments).
Concept Snapshot
strpos(string, search) returns the position of search in string (0-based index).
strstr(string, search) returns substring from search to end.
If search not found, strpos returns false.
Use === to check strpos result to avoid confusion with 0.
strstr returns false if not found.
Useful for finding or extracting parts of strings.
Full Transcript
This example shows how PHP string search functions strpos and strstr work. We start with a string 'hello world' and search for 'world'. strpos returns 6 because 'world' starts at index 6 (counting from zero). strstr returns the substring 'world' from that position to the end. If the search term is not found, strpos returns false, so you should check the result carefully. strstr also returns false if not found. These functions help find positions or parts of strings easily.