note 114687 deleted from function.strpos by cmb

From: Date: Sun, 02 Jun 2019 08:01:59 +0000
Subject: note 114687 deleted from function.strpos by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-211166@lists.php.net to get a copy of this message
Note Submitter: m at binh dot vn ---- Using strpos can be tricky sometimes. To reuse it for one search you can use this form: <?php $found = (strpos($haystack, $needle) !== false); ?> For example: <?php $url = "http://www.binh.vn"; $found = (strpos($url, 'http') !== false); if ($found) echo "Found http"; ?> Clearly we see that the position will be 0 but the $found will be true and so we can use more than one time of if ($found) which is much easier. To further reuse it we could make a very short function: <?php function found($haystack, $needle) { return (strpos($haystack, $needle) !== false); } ?> Wonder why php didn't have such function for easier use?

« previous php.notes (#211166) next »