$position=strpos("Welcome",'e');
echo $position; // Output is 1
$int_position=strpos(Main_string, Search_string,[optional: offset =0 ]);
Main_string : The string inside of which the search_string will be searched. $position=strpos("Welcome",'e',2);
echo $position; // Output is 6 , not 1
$position=strpos("Welcome",'z');
if($position==false){
echo " Not found ";
}else{
echo " Found and position = ".$position;
}
// Output is Not found
Using the same code we will search for string W
$position=strpos("Welcome",'W');
if($position==false){
echo " Not found ";
}else{
echo " Found and position = ".$position;
}
// Output is Not found
The search string is already present at position 0 . Above code need to change like this to get correct output.
$position=strpos("Welcome",'W');
if($position===false){
echo " Not found ";
}else{
echo " Found and position = ".$position;
} // Output is Found and position = 0
$position=strpos("Welcome",'M');
if($position===false){
echo " Not found ";
}else{
echo " Found and position = ".$position;
} // Output is Not Found
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.