How to use strpos() function in PHP?

Strpos() – Find the position of first occurrence of a sub sting in a string.

Syntax - strpos( string , find , start )

string – Specifies the string to search

find – Specifies the string to find

Start – it’s optional, from where to start

<?php
$name = "gaurav k";
$position = strpos( $name, "u" );
print $position;
?>

The output of this will be 2. As it starts from position 0 and finds “u” at position 2.It return false if nothing found.

For Example – If you replace “u” with “U” in the above example to find position then there will be nothing displayed in output because of false statement.

<?php
$name = "gaurav k";
$position = strpos( $name, "U" );

if($posiiton === false)
{
print "not found" ;
}
else
{
 print "found" ;
}?>

The triple equals operator ( === ) not only checks for a value but what type of value it is: integer, string, Boolean, etc

Leave a Reply