PHP string goes empty unless echoed
I have the following test program:
#!/usr/bin/php
<?php
echo "Starting\n";
# Now read the file.
#
$file_pn = "contact.txt";
$fh = fopen($file_pn, 'r');
while (!feof($fh)) {
$line = fgets($fh);
$trimmed = trim($line);
echo "trimmed = <" . $trimmed . ">\n";
if (preg_match('/^\s*$/', $trimmed)) {
echo "Looks blank: trimmed = <" . $trimmed . ">\n";
#continue;
}
}
fclose($fh);
?>
The file being read has some blank lines and some non-blank lines. As
shown here, it acts as one would expect: only the lines that are actually
blank receive the "Looks blank" message, and trimmed = <>. But if I
comment out the echo statement in line 12 and run the script again,
$trimmed always appears to be the empty string when we get to the if
statement. It is as if $trimmed gets clobbered unless it has been echoed.
How can this be?
No comments:
Post a Comment