Bird
Raised Fist0
PHPprogramming~10 mins

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

Choose your learning style10 modes available
Concept Flow - String search functions (strpos, strstr)
Start with string and search term
↓
Call strpos or strstr
↓
Check if search term found?
No→Return 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.

Practice

(1/5)
1. What does the PHP function strpos do?
easy
A. Converts a string to uppercase
B. Extracts a substring starting from a specific character
C. Finds the position of a substring inside a string
D. Replaces a substring with another string

Solution

  1. Step 1: Understand the purpose of strpos

    The strpos function searches for the first occurrence of a substring inside another string and returns its position.
  2. Step 2: Compare with other options

    Options A, B, and C describe different string functions like strtoupper, strstr, and str_replace, which are not what strpos does.
  3. Final Answer:

    Finds the position of a substring inside a string -> Option C
  4. Quick Check:

    strpos = position finder [OK]
Hint: strpos returns position index of substring [OK]
Common Mistakes:
  • Confusing strpos with strstr
  • Thinking strpos extracts substring
  • Assuming strpos replaces text
2. Which of the following is the correct syntax to find the position of 'cat' in the string $text = 'concatenate' using strpos?
easy
A. strpos('concatenate', 'cat')
B. strpos($text)
C. strpos('cat', $text)
D. strpos($text, 'cat')

Solution

  1. Step 1: Identify correct parameter order for strpos

    The first parameter is the full string, the second is the substring to find. So strpos($text, 'cat') is correct.
  2. Step 2: Check other options for errors

    strpos('concatenate', 'cat') uses a string literal instead of the variable. strpos('cat', $text) reverses parameters. strpos($text) misses the second parameter.
  3. Final Answer:

    strpos($text, 'cat') -> Option D
  4. Quick Check:

    strpos(string, substring) [OK]
Hint: First argument is full string, second is substring [OK]
Common Mistakes:
  • Swapping the order of parameters
  • Omitting the substring argument
  • Passing substring as first argument
3. What will be the output of this PHP code?
$text = 'hello world';
$pos = strpos($text, 'world');
echo $pos;
medium
A. 6
B. 5
C. 0
D. false

Solution

  1. Step 1: Find the position of 'world' in 'hello world'

    The string 'hello world' has 'world' starting at index 6 (counting from 0).
  2. Step 2: Understand what strpos returns

    strpos returns the starting position of the substring, so it returns 6.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Position of 'world' in 'hello world' = 6 [OK]
Hint: Count characters from zero to substring start [OK]
Common Mistakes:
  • Counting from 1 instead of 0
  • Confusing position with length
  • Expecting boolean true instead of position
4. The following code is intended to check if the word 'apple' exists in $fruit. What is the error?
$fruit = 'pineapple';
if (strpos($fruit, 'apple')) {
echo 'Found';
} else {
echo 'Not found';
}
medium
A. strpos returns false if found, so condition is wrong
B. strpos returns 0 for 'apple' position, which is treated as false
C. Missing third parameter in strpos
D. Syntax error in if statement

Solution

  1. Step 1: Understand strpos return values

    strpos returns 0 if the substring is found at the start (index 0). In PHP, 0 is treated as false in conditions.
  2. Step 2: Analyze the if condition behavior

    The condition if (strpos(...)) fails when strpos returns 0, so it wrongly goes to else.
  3. Final Answer:

    strpos returns 0 for 'apple' position, which is treated as false -> Option B
  4. Quick Check:

    Check strpos !== false to avoid 0 as false [OK]
Hint: Check strpos !== false, not just if() [OK]
Common Mistakes:
  • Using if(strpos()) without strict false check
  • Assuming strpos returns boolean true/false only
  • Ignoring 0 as valid position
5. Given the string $sentence = 'The quick brown fox jumps', which code snippet correctly extracts the substring starting from the word 'brown' using strstr?
hard
A. echo strstr($sentence, 'brown');
B. echo strstr($sentence, 'brown', true);
C. echo strpos($sentence, 'brown');
D. echo substr($sentence, strpos($sentence, 'brown'));

Solution

  1. Step 1: Understand strstr behavior

    strstr returns the part of the string starting from the first occurrence of the substring, including it.
  2. Step 2: Analyze options

    echo strstr($sentence, 'brown'); correctly uses strstr to get substring from 'brown'. echo strstr($sentence, 'brown', true); returns part before 'brown'. echo strpos($sentence, 'brown'); returns position, not substring. echo substr($sentence, strpos($sentence, 'brown')); uses substr with strpos, which also works but is not strstr.
  3. Final Answer:

    echo strstr($sentence, 'brown'); -> Option A
  4. Quick Check:

    strstr returns substring from needle [OK]
Hint: strstr returns substring from needle, true returns before [OK]
Common Mistakes:
  • Using third parameter true to get substring after needle
  • Confusing strpos with strstr
  • Expecting strpos to return substring
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard