<?php //specify the filename and we call it as hello.txt $testfile = 'hello.txt'; //and inside the if statement, we pass the $testfile variable to fopen and we specify the mode as write //and fopen return a handle as a value, and using handle is useful because we could close it //returns number of bytes or false //close the handle }else{ //if the file can't be open..and it can prevent some error might happen echo 'Could not open the file'; } ?>
fwrite($fh, "hello");
and then the cursor can be found at the side of the letter “o” and then when add another fwrite($fh, "World");
it append the second word to our first word.
Here’s the another example.
Here in our new code you have noticed that in order for us to move the “world” into the next line we use these $greetings = "hello\r\nWorld";
because we are using a windows platform. And for UNIX you can simply use $greetings = "hello\nWorld";</
.
<?php //specify the filename and we call it as hello.txt $testfile = 'hello.txt'; //and inside the if statement, we pass the $testfile variable to fopen and we specify the mode as write //and fopen return a handle as a value, and using handle is useful because we could close it //returns number of bytes or false $greetings = "hello\r\nWorld";//double quote matters //close the handle }else{ //if the file can't be open..and it can prevent some error might happen echo 'Could not open the file'; } ?>