Tools, FAQ, Tutorials:
Issue with "while ($c=fgetc($f))" Loop in PHP
What's Wrong with "while ($c=fgetc($f)) {}" in PHP?
✍: FYIcenter.com
If you are using "while ($c=fgetc($f)) {}" to loop through each character in a file, the loop may end in the middle of the file when there is a "0" character, because PHP treats "0" as Boolean false. To properly loop to the end of the file, you should use "while ( ($c=fgetc($f)) !== false ) {}". Here is a PHP script example on incorrect testing of fgetc():
<?php $file = fopen("/temp/cgi.log", "w"); fwrite($file,"Remote host: 64.233.179.104.\r\n"); fwrite($file,"Query string: cate=102&order=down&lang=en.\r\n"); fclose($file); $file = fopen("/temp/cgi.log", "r"); while ( ($char=fgetc($file)) ) { print($char); } fclose($file); ?>
This script will print:
Remote host: 64.233.179.1
As you can see the loop indeed stopped at character "0".
Â
⇒Reading and Writing Files in PHP
⇒⇒PHP Tutorials
2016-11-27, 2504👍, 0💬
Popular Posts:
What is Azure API Management Gateway? Azure API Management Gateway is the Azure Web server that serv...
How to use the urllib.request.Request object to build more complex HTTP request? The urllib.request....
Where to find tutorials on EPUB file format? I want to know how to create EPUB books. Here is a larg...
Where to find tutorials on Using Azure API Management Developer Portal? Here is a list of tutorials ...
What is EPUB 2.0 Metadata "dc:creator" and "dc:contributor" elements? EPUB 2.0 Metadata "dc:creator"...