Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 7 of 10 · Arrays & String Functions
Interview question

What is the difference between strpos() and stripos()? strpos() और stripos() में क्या अंतर है?

Answer

Both find the position of the first occurrence of a substring within a string. The only difference is case sensitivity.

FunctionCase sensitive?
strpos()Yes
stripos()No (case-insensitive)
strpos('Hello World', 'world');  // false, case mismatch
stripos('Hello World', 'world'); // 6, found ignoring case

Interview tip: Warn about the classic bug: strpos() can return 0 (a valid position) which is falsy in loose comparisons. Always check with === false instead of if (!strpos(...)) to correctly detect 'not found'.

if (strpos($str, 'needle') === false) {
    echo 'Not found';
}

दोनों string में substring की पहली occurrence की position ढूंढते हैं। इनमें सिर्फ case sensitivity का अंतर है।

FunctionCase sensitive?
strpos()हाँ
stripos()नहीं (case-insensitive)
strpos('Hello World', 'world');  // false, case mismatch
stripos('Hello World', 'world'); // 6, case ignore करके मिला

इंटरव्यू टिप: क्लासिक बग की चेतावनी दें: strpos() 0 रिटर्न कर सकता है (एक valid position) जो loose comparison में falsy है। 'not found' सही detect करने के लिए हमेशा === false इस्तेमाल करें, if (!strpos(...)) नहीं।

if (strpos($str, 'needle') === false) {
    echo 'Not found';
}

Was this answer clear?