Javascript là một ngôn ngữ thông dịch (interpreter), chương trình nguồn của nó được nhúng (embedded) hoặc tích hợp (integated) vào tập tin HTML chuẩn. Khi file được load trong Browser (có support cho JavaScript), Browser sẽ thông dịch các Script và thực hiện các công việc xác định. Chương trình nguồn JavaScript được thông dịch trong trang HTML sau khi toàn bộ trang được load nhưng trước khi trang được hiển thị | The n puts a line break into the file which creates a two-line file in this case. If the write is successful success will contain the value TRUE if not it will contain the value FALSE. Once you ve written to a file close it with fclose fclose myFile Combining these lines gives you the PHP script below php myFile fopen w success fwrite myFile line one nline two fclose myFile if success TRUE print Write succeeded else print Write failed One of the more pernicious problems you ll encounter when dealing with server-side programming is that of ensuring that your webserver has permission to alter the files you want to alter. Sometimes a PHP program can fail to write to a file because the webserver running it does not have access to that particular file. If this happens PHP will give an error like this Warning fopen function fopen failed to open stream Permission denied. File permissions work differently on different operating systems. If you get this kind of error refer to your operating system s manuals to determine how to inspect and modify file permissions. Reading Files in PHP It s a bit trickier to read a file using PHP than it is to write to a file. The complication arises because the contents of a file are read line by line and PHP needs to know when it has reached the end of a file so that it can stop reading. Luckily the PHP function feof will tell PHP when it has reached the end of a file. This function takes a variable that points to an open file such as myFile and returns TRUE when the end of the file has been reached. Figure 16-13 shows an example of PHP reading a file. php o myFile fopen r contents while feof myFile FALSE contents contents . fgets myFile 322 Chopter 16 0 fclose myFile print The file s contents are . contents Figure 16-13 Reading a file with PHP In Figure 16-13 opens the file for reading and puts a pointer to the opened file into the variable myFile. The most complicated line is which calls the feof .