What Is The Best Way To Test The Strpos() Return Value In PHP

What Is The Best Way To Test The Strpos() Return Value In PHP


  • 21 Feb, 2012

    Because strpos() could two types of values, Integer and Boolean, you need to be careful about testing the return value. The best way is to use the "Identical(===)" operator. Do not use the "Equal(==)" operator, because it does not differentiate "0" and "false". Check out this PHP script on how to use strpos():<?php$haystack = "needle234953413434516504381640386488129";$pos = strpos($haystack, "needle");if ($pos==false) { print("Not found based (==) test");} else { print("Found based (==) test");}if ($pos===false) { print("Not found based (===) test");} else { print("Found based (===) test");}?>This script will print:Not found based (==) testFound based (===) testOf course, (===) test is correct.

    Comments Received:
    Please give your suggestions and feedback:

2009-2016 downloadmela.com. All rights reserved.