note 92598 added to function.array-search

From: Date: Fri, 31 Jul 2009 00:50:53 +0000
Subject: note 92598 added to function.array-search
Groups: php.notes 
Request: Send a blank email to php-notes+get-158933@lists.php.net to get a copy of this message
re: greenmr: "Some solutions I've seen here and elsewhere return the index of the found element on a match, and "false" on a fail. Since PHP treats a zero the same as false in expressions, and zero is a valid array index, using false to designate "not found" gives an ambiguous result in some cases. I chose to use the value -1 to signify a search failure instead." You can cope with the 0/false ambiguity simply by making sure to do type-safe comparisons with === and !==. e.g.: <?php if(($i = binarySearch($array)) !== false) { //we had a match } ?> This is how some PHP functions like array_search() work (if you look at the doc for array_search(), there's a warning to make sure to use the same comparison operators). The contributors you refer to were likely modeling their functions on that example. ---- Server IP: 64.71.164.2 Probable Submitter: 75.101.52.104 ---- Manual Page -- http://www.php.net/manual/en/function.array-search.php Edit -- https://master.php.net/note/edit/92598 Del: integrated -- https://master.php.net/note/delete/92598/integrated Del: useless -- https://master.php.net/note/delete/92598/useless Del: bad code -- https://master.php.net/note/delete/92598/bad+code Del: spam -- https://master.php.net/note/delete/92598/spam Del: non-english -- https://master.php.net/note/delete/92598/non-english Del: in docs -- https://master.php.net/note/delete/92598/in+docs Del: other reasons-- https://master.php.net/note/delete/92598 Reject -- https://master.php.net/note/reject/92598 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#158933) next »