Just wondering what is going wrong here, have been scratching my head for way too long on this. This is a script being called by another php script and it works correctly until the last bit where it is supposed to write 0.0 to the file. Both - 1 and 1 are written to the file but not the 0.0 . I don't know what I am doing wrong. The output file is just blank. I disabled the last bit and I see -1 and 1 perfectly, but when I enable the 0 part nothing is there in he file.
Also are these values integers or characters? I am asking this since another program is going to be reading the newfile.txt and taking the data out.
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
if ($_POST['leftb'])
{ echo "Left is pressed";
$txt = "-1 \n";}
else if ($_POST['rightb'])
{ echo "Right is pressed";
$txt = "1 \n";}
fwrite($myfile, $txt); // Write either -1 or 1 to the file
fclose($myfile); // Close the file
sleep(2);
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); // Open It again
$txt = "0.0 \n";
write($myfile, $txt); // Write 0 to the file
fclose($myfile);
?>
You have mistyping
Change like this. write($myfile, $txt) => fwrite($myfile, $txt)
Everything works good.
at the beginning of code
Add two line.
error_reporting(E_ALL);
ini_set("display_errors", 1);
This will tell you what is wrong.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.