In many cases, front-end programs need to generate. php files, such as multi-user online diaries, message books, and self-help website programs, which inevitably generate. php program files during interaction with users. Generally, the generated. php file is not complex, but the complete. php file structure must be ensured, although it is not complex. A. php file that contains the program start character (). the specific code is placed between the start character and the end character. In order to facilitate future modification and program reading, traditionally, code should be divided according to certain regulations, while php code must have a variable identifier ($ ). All of these problems cause some trouble to generate. php files, and some workarounds are required to process them.
Let's first process the start and end characters of the php program file. this is not complicated. We use two variables to represent them:
$ Str_tmp =" $ Str_end = "?> "; // Terminator. $ Str_end is an independent variable.
Let's process the variable identifier ($). let's see a variable equation in the generated. php file: $ age = "28 ";. The code is as follows:
$ Str = "$". "age = \" 28 \ "; \ r \ n ";
In the above formula, print the variable $ str to get the expected effect. In the code, $ and age must be separated and used with the connection number (.) combine it to get the effect of $ age. the symbol (\ ") indicates a small double quotation mark ("); the end of a php code line must contain a semicolon (;); \ r \ n indicates to press enter to start another line.
The above solves the php code start character, end character, code variable sign, equation and other input problems, now you can go to the specific operation to generate the. php file. To make the program look like this, we preset the content of the. php file to be generated: user name (Dark Horse), logon password (blackhorse), age (28), Revenue (4500 ). The complete code for generating the. php file is as follows:
// Collect information
$ Str_tmp =" $ Str_end = "?> "; // Php Terminator
$ Str_tmp. = "$". "name = \" \ "; \ r \ n"; // add user name
$ Str_tmp. = "$". "pass = \" blackhorse \ "; \ r \ n"; // add the password
$ Str_tmp. = "$". "age = \" 28 \ "; \ r \ n"; // add to age
$ Str_tmp. = "$". "income = \" 4500 \ "; \ r \ n ";
$ Str_tmp. = $ str_end; // add the Terminator.
// Save the file
$ Sf = "blhorse. php"; // file name
$ Fp = fopen ($ sf, "w"); // open a file in write mode
Fwrite ($ fp, $ str_tmp); // store the content
Fclose ($ fp); // close the file
?>
Save the above code as a file named test. php and run it to get the blhorse. php file. the complete content of this file is as follows:
$ Name = "";
$ Pass = "blackhorse ";
$ Age = "28 ";
$ Income = "4500 ";
?>
We can see that what we get is a fairly standard. php configuration file that fully meets our requirements. As for writing more complex. php files, it is quite easy to implement as long as the assignment and accumulation of variables are handled as shown in the code in this article.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service