I've got a crazy problem with trying to write out text to a file (a .csv file as it happens, but that should be immaterial).
Here is version 1 -
$newvariable = "widnes";
$strlength = strlen($newvariable);
echo "length of name output : " . $strlength . "<br>";
$outputline = "x" . $strlength . "x";
echo $outputline . "<br>";
$writesuccess = fwrite($handle, $outputline);
echo "write result : " . $writesuccess . "<br>";
echo "length of name output : " . $strlength . "<br>";
$outputline = "x,,,,,,\r\n";
fwrite($handle, $outputline);
The results are, on the screen -
length of name output : 6
x6x
write result : 3
length of name output : 6
And in the file -
x6xx
And here is version 2 -
$newvariable = $locationoutput;
$strlength = strlen($newvariable);
echo "length of name output : " . $strlength . "<br>";
$outputline = "x" . $strlength . "x";
echo $outputline . "<br>";
$writesuccess = fwrite($handle, $outputline);
echo "write result : " . $writesuccess . "<br>";
echo "length of name output : " . $strlength . "<br>";
$outputline = "x,,,,,,\r\n";
fwrite($handle, $outputline)
The results are, on the screen -
length of name output : 7
x7x
write result : 3
length of name output : 7
And in the file -
x0xx
Note that the only difference in the two bits of source code are the initial lines (in version 2 it should be being set to 'Glasgow' earlier in the script hence the length = 7 outputs.
The problem was originally that I was trying to output the text (i.e. 'Glasgow') but it wasn't outputting anything. So, thinks I, I'll just output the length of the strings instead (and set the string length on a separate line in case there is a problem somewhere there). But - even though I'm only trying to output an integer value held in a variable, in one version it outputs the value and in the other it outputs zero instead! Even though it is outputting the correct value to the screen...
Incidentally, the whole script (in all its buggy, filled with debug code and bad style glory) is viewable from
http://www.fullcharge.co.uk/rob/report.txt
if it is any clearer.
Help! 😕