It finds the position of the first occurrence of a substring in a string. If the substring is found, it returns the index (starting at 0). If not found, it returns false.
Click to reveal answer
beginner
What is the difference between strpos and strstr?
strpos returns the position (number) of the substring in the string. strstr returns the part of the string starting from the first occurrence of the substring to the end.
Click to reveal answer
beginner
What does strstr return if the substring is not found?
It returns false if the substring is not found in the string.
Click to reveal answer
intermediate
How does strpos handle the case when the substring is at the very start of the string?
It returns 0 because the substring starts at the first character (index 0). Remember, 0 is a valid position, not false.
Click to reveal answer
intermediate
How can you safely check if a substring exists using strpos?
Use the strict comparison operator !== false because strpos can return 0 (which is falsey). For example: if (strpos($text, 'word') !== false) { /* found */ }
Click to reveal answer
What does strpos('hello world', 'world') return?
A6
B0
Cfalse
D11
✗ Incorrect
The substring 'world' starts at index 6 in 'hello world'.
What will strstr('hello world', 'world') return?
A'world' plus the rest of the string starting from 'world'
B'hello world'
C'world' plus the rest of the string
D'world'
✗ Incorrect
strstr returns the part of the string starting from the first occurrence of the substring to the end, so it returns 'world'.
What does strpos('apple', 'a') return?
Afalse
B0
C1
D5
✗ Incorrect
The substring 'a' is at the start of 'apple', so the position is 0.
If strpos returns 0, what does that mean?
ASubstring found at the end
BSubstring not found
CError in function
DSubstring found at the start
✗ Incorrect
0 means the substring is found at the very start of the string.
Which is the correct way to check if 'cat' is in a string $text?
Aif (strpos($text, 'cat') == true) { ... }
Bif (strpos($text, 'cat')) { ... }
Cif (strpos($text, 'cat') !== false) { ... }
Dif (strpos($text, 'cat') == false) { ... }
✗ Incorrect
Use strict comparison !== false to correctly detect substring presence, even if at position 0.
Explain how strpos works and how to correctly check if a substring exists in a string.
Think about what happens if the substring is at the start.
You got /4 concepts.
Describe the behavior of strstr and what it returns when the substring is found or not found.
It returns a string, not a number.
You got /3 concepts.
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
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.
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.
Final Answer:
Finds the position of a substring inside a string -> Option C
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
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.
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.
Final Answer:
strpos($text, 'cat') -> Option D
Quick Check:
strpos(string, substring) [OK]
Hint: First argument is full string, second is substring [OK]
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
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.
Step 2: Analyze the if condition behavior
The condition if (strpos(...)) fails when strpos returns 0, so it wrongly goes to else.
Final Answer:
strpos returns 0 for 'apple' position, which is treated as false -> Option B
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
Step 1: Understand strstr behavior
strstr returns the part of the string starting from the first occurrence of the substring, including it.
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.
Final Answer:
echo strstr($sentence, 'brown'); -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.