$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 & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.