Path: csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.php Subject: Re: Looking for NULL in arrays Date: Tue, 27 Oct 2015 22:23:23 +0100 Organization: PointedEars Software (PES) Lines: 40 Message-ID: <5273400.d3j8xVGaf9@PointedEars.de> References: Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1445981004 13947 eJwFwYEBwCAIA7CXqFCUc1iV/09YQk+kdiQzOBwDmi7vFygyz2BNQDZdTw3ts6JG/qVu2/0BEaARTg== (27 Oct 2015 21:23:24 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Tue, 27 Oct 2015 21:23:24 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwNyskRwDAIBLCWYNjlKAdj038Jid6iufoEnA4u1zSqJ/VeU4BV+ygYASM1E5tqm90mhTp7wlNf/TtOUXY+K+4Uww== Cancel-Lock: sha1:VtyrJml0b/J/O3vdETD4rnW1fec= X-NNTP-Posting-Host: eJwFwYEBwDAEBMCVKP9hnAr2HyF3MCrvcYKOxRpyIeEazTjs+ce/olnoojJVyK7hiNy87AcH3RC2 Xref: csiph.com comp.lang.php:15799 Denis McMahon wrote: > On Sun, 25 Oct 2015 16:05:19 +0000, Derek Turner wrote: >> that will detect a NULL value and stop. ^^^^^^^^^^ > Works with the test cases shown, but otherwise untested so no guarantees > of any specific behaviour if you feed it garbage. > > > $a = array( > array("fred","jim","peter","susan","mary",0), > array("fred","jim","susan","mary",0), > array("fred","susan","mary",0), > array("fred","mary",0), > array("fred",0), > array("fred","jim","susan","mary",0,0), > array("fred","susan","mary",0,0,0), > array("fred","mary",0,0,0,0), > array("fred",0,0,0,0,0) > ); Flawed testcase: There is not a single occurrence of “null” or “NULL” there. > foreach ($a as $arr) { > $idx = 0; > while ($idx < count($arr)-1 && $arr[$idx] != NULL) $idx += 1; “!= null” is a comparison recommended against as it does type juggling. In PHP, “null” (or “NULL”) is a special value. It is *that value* that were to be detected. And the requirement was to do this *without* “having to use an 'if' or similar test”, which your approach obviously does not meet. (The reason for that is that it cannot be met.) -- PointedEars Zend Certified PHP Engineer Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail.