This tutorial will help you to check if a string contains any substring in PHP programming language. For example, you want to run a specific line of code only if an input string contains another substring in it. Here is a sample PHP programme, which will evaluate to true because the main string $str contains the substring ‘TecAdmin’ in it. This will print “true”.
1 2 3 4 5 6 7 | <?php $str = 'Welcome to Tecadmin'; if (strpos($str, 'Tecadmin') !== false) { echo 'true'; } ?> |
Another PHP program will evaluate to false because the main string $str doesn’t contains the substring ‘Hello’ in it. This will nothing print.
1 2 3 4 5 6 7 8 | <?php $str = 'Welcome to Tecadmin'; $substr = "Hello"; if (strpos($str, $substr) !== false) { echo 'true'; } ?> |