Doc #55136 [Opn->Bgs]: Code sample is wrong

From: Date: Tue, 05 Jul 2011 14:20:34 +0000
Subject: Doc #55136 [Opn->Bgs]: Code sample is wrong
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-6834@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=55136&edit=1 ID: 55136 Updated by: RQuadling@php.net Reported by: rp_joe at yahoo dot com Summary: Code sample is wrong -Status: Open +Status: Bogus Type: Documentation Problem Package: *General Issues Operating System: xp PHP Version: Irrelevant Block user comment: N Private report: N New Comment: $buffer is assigned a value from fgets(). If that value is not false (both in type and in value), then $buffer is displayed. fgets() cannot assign a boolean false for valid data. false indicates a failure of some sort or end of file. Due to type juggling, '' and 0 can both be juggled to false and therefore would cause the while loop to abort, not printing $buffer. Therefore, !== is used to enforce both the value and the type during the comparison. Previous Comments: ------------------------------------------------------------------------ [2011-07-05 09:52:43] rp_joe at yahoo dot com Description: ------------ --- From manual page: http://www.php.net/function.fgets%23Examples --- This code sample is wrong! while (($buffer = fgets($handle, 4096)) !== false) { echo $buffer; } When the !== false is used it returns a value, whether the function failed or not. In this example $buffer will be 1 or 0. Test script: --------------- while ($buffer = fgets($handle, 4096)) { echo $buffer; } Expected result: ---------------- I expect to see a line of text read from the file and echoed. Actual result: -------------- 0 or 1 is echoed. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=55136&edit=1

« previous php.doc.bugs (#6834) next »