How to write to php file

If you learn php yourself, it’s important to learn how to read data from a file and enter changes into it. Writing to the php file is for developing web applications, changing server information and launching external programs and inclusions.

How to work with php files

write to php file
If the file has such an extension, then it means it contains program code written in the programming language of the same name. In order to make changes, you will need to install one of the program editors on your PC:

β€’ PHP Expert Editor;
β€’ Dreamweaver;
β€’ Eclipse PHP Development;
β€’ PHPEdit.

If you are creating a website, you often need to use identical designs that are conveniently stored as templates in another file. To do this, it is better to use include. After entering this function, you need to write the name and extension of the connection to the php file, for example, include 1.php. This design supports the ability to read the included file multiple times, and an additional feature is the continuity of code execution on error. If something goes wrong, the code will continue to execute from a new line.
This way you attach the file to your code. Another way is to enter require. Unlike the above include, the file is included before the start of the program code execution, but you can access it only once.

File check

php write to text file
Before you write to the php file, you need to make sure that it exists, and then open it and do the necessary editing. To do this, you can use the file_exists () function, if it confirms the presence, then the TRUE response will be returned in the editor window, otherwise FALSE.
Another function is_file () may notify you that data can be written to the php file. It is more reliable than file_exits, and most programmers use is_file to get started. After you have verified that the file exists, you can start working with it.

Making changes to the file

writing data to php file
The first tool you can use to make changes to the file is fwrite (). It writes lines with the following syntax:

β€’ Int - manipulator;
β€’ String is a variable.

There is another similar to this function - fputs (), which is an alias. There are no differences between these tools; one or the other can be used optionally. For students, fwrite is more common, and practicing programmers most often use fputs.

To make a php record in a text file, one important condition must be met - it must be open for editing. Such a file should be located in the folder with write permissions.

To work, try first opening a text document in the program, and then make changes using the usual tools.

<? php
// Open a text file
$ f = fopen ("text.txt", "w");
// Write a line of text
fwrite ($ f, "Hello! Good day!");
// Close the text file
fclose ($ f);

In this example, text.txt is the name of the document. You can call it differently: β€œHello! Good day! ” - an example of a text, it can be completely arbitrary. All changes are automatically saved in an open file.

Note that PHP is a general-purpose scripting language . It is heavily used to create web applications. This solution is supported by most hosting providers. This is a leader among the tools that are used to create websites. Now you know how to write to php file.

Source: https://habr.com/ru/post/C18667/


All Articles