PHP strpos Function | What is strpos in PHP and How to use to find string

strpos Summary: in this tutorial, you will learn What is PHP strpos Function and How to use to find the position of the first occurrence of a string in another string.

Definition and Usage PHP strpos() Function

This function helps us to find out the position of the first occurrence of a string in another string. This function returns an integer value of the position of the first occurrence of the string. also, this function is case-sensitive, which implies that it treats upper-case and lower-case characters differently.

.
Note: The strpos() function is case-sensitive, treated as upper-case and lower-case characters differently.
This function is binary-safe.
Related functions:

strrpos() – Finds the position of the last occurrence of a string inside another string. this is a case-sensitive function.
stripos() – Finds the position of the first occurrence of a string inside another string. this is a case-insensitive function.
strripos() – Finds the position of the last occurrence of a string inside another string. this is also a case-insensitive function.

Syntax:

strpos(original_str, search_str, start_pos)
Parameters used:

there are three parameters in the syntax, two-parameters are mandatory and one parameter is optional. These three parameters are mentioned below:

PARAMETER DESCRIPTION
original_str (Required): This parameter refers to the original string in which we need to search the occurrence of the required string.
search_str (Required): This parameter refers to the string that we need to search
start_pos (optional): Refers to the position of the string from where the search must begin.

Return Type: This function returns an integer value that represents the index of original_str where the string search_str first occurs.

Technical Details

Title DESCRIPTION
PHP Version: 4+
Changelog: PHP 7.1.0 – The start/beginning parameter can be a negative number
Return Value: Returns the position of the first occurrence of a string inside another string, or FALSE if the string isn’t found. Important Note: String positions start/begin at 0, and not 1.

Example of PHP strpos Function


Found at position of string: 30

PHP strpos Function Related Question and Answer:

What is the strpos () function used for?
Answer: The strpos() function used when we need to finds the position of the first occurrence of a string inside another string.


Is Strpos case sensitive?
Answer: This function returns an integer value of the position of the first occurrence of the string. it is the case-sensitive function, which means that it treats upper-case & lower-case characters differently.


What does Strpos return if not found?
Answer: Returns false if the needle was not found. This function may return Boolean false, but may also return a non-Boolean value which evaluates to false.