note 87868 deleted from function.strpos by cmb

From: Date: Sun, 02 Jun 2019 09:01:02 +0000
Subject: note 87868 deleted from function.strpos by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-211174@lists.php.net to get a copy of this message
Note Submitter: jburns131 at jbwebware dot com ---- The Situation: I wanted to return TRUE if strpos returned position 0, and only position 0, without the added overhead of preg_match. The Solution: As PHP treats 0 (zero) as NULL, and strpos returns the int 0 (but not NULL), I used type casting and the "===" comparison operator (as it compares types) to resolve my issue. <?php $hayStack = "dbHost"; $needle = "db" $needlePos = strpos($hayStack, $needle); if((string)$needlePos === (string)0) { echo "db is in position zero"; } else { echo "db is NOT in position zero"; } ?> Returns: db is in position zero <?php $hayStack = "another_db_host"; $needle = "db" $needlePos = strpos($hayStack, $needle); if((string)$needlePos === (string)0) { echo "db is in position zero"; } else { echo "db is NOT in position zero"; } ?> This returns: db is in NOT position zero

« previous php.notes (#211174) next »