This article is original and copyrighted by the man who was knocked down by the wind. It comes from the forum of www.ipp.org. Please keep this information for online reprinting. Please contact me for non-online reprinting. It is not easy for individual webmasters to write things by themselves. , and to give me the courage and motivation to continue writing, please do not delete this description when reprinting. This article is the first one, and I will find time to write out the rest one after another.
Many friends’ understanding of PHP has been stuck in modifying other people’s programs for a long time. Because they can be modified, they are too lazy to make progress. Or when they bought a book and read it, they found that PHP has so many functions. I was scared to death when I saw the function library. Wait. I belong to the latter. This article is specifically for lazy people who spend time learning PHP, but want to make something by themselves.
Later, I joined a minor class in school. The teacher taught in class and I had to take the final exam. So, I was forced to write simple code by myself.
So , this article is for friends who can modify the HTML in the program, but cannot modify the program structure. They have basic programming foundation, that is: being able to understand if and else. It is enough, or everyone with the same foundation is suitable. Experts can go Open. Until now, I still need to check the manual with common functions, and I still write garbage code.
Okay, let's talk less, let's enter the first stage
Friends who can modify programs are considered web masters in the eyes of their classmates. If I start talking about what variables are, it will inevitably waste your time. I myself am a text collector, so we can start with Start by analyzing a part of a program in detail. The program is very simple, with only a few simple judgments. I will write the description of the program in unparalleled detail.
Quote:
If you don't understand something, please tell me and I will correct it as soon as possible, or email me. My email address is rainboy#tom.com. Please don't send me spam. I have been spammed now. It’s very annoying
Let’s start by analyzing the simplest counter
The function of the counter is: refresh the page once, the number increases by one.
The program is as follows
Code:
[Copy to clipboard]
$datafile="data.txt";
$fp1=fopen($datafile,"r");
$num=fgets($fp1,10);
$num =trim($num);
fclose($fp1);
echo "You are the ".$num." guest";
$fp2=fopen($datafile,"w");
$num=$num+1;
fputs($fp2,"$num");
fclose($fp2);
?>
Here is a detailed explanation of the procedure
Code:
[Copy to clipboard]
/* PHP files are wrapped with ?> or . When the server executes, the content in the tag can be taken out and executed.* /
/*
Set the data storage file, because the variables in php are stored in the server memory and disappear immediately after execution, so we need to use text to save the number of visitors who browse , we use data.txt, we can adjust it according to the situation. There is a $ sign in front of the variables in php, so everything with the $ sign is a variable. Create the data file manually, write 1 in it, and then save it
*/
$datafile="data.txt";
/*This step is to open the file, using the fopen() function, which is the function to open the file. It accepts two parameters, one is the file name and the other is the opening method. For example, if you use "r" to open in this program, it will be opened in read-only mode. $fp1 is a handle. What is a handle? Just like when you use a toothbrush , by using the handle of the toothbrush to operate the toothbrush to make your teeth clean. Then $fp1 is like the handle of the toothbrush. PHP controls the data storage file data.txt by operating the handle of $fp1. This step operates To sum it up in one sentence: use the fopen function to open the data file, and pass the handle of the opened file to $fp1. If you still have questions about the use of the fopen function after reading this, please refer to this site's PHP online manual http: //www.ipp.org/manual/function.fopen.html
How to use the fopen function
*/
$fp1=fopen($datafile,"r");
/*
To summarize the use of this statement in one sentence: open the file to read data
Use the function: fgets("handle", "number of bytes to read the file content");, and get the result The purpose of reading the file content is to read the handle $fp1. The $num obtained in this sentence is the first 10 bytes in the file.
*/
$num=fgets($fp1,10);
/*
trim(); function is used to remove spaces before and after a string, for example,
$num="123 ";
trim($num) is 123 ;
*/
$num=trim($num);
/*
The fclose function closes the file. The parameter required by the fclose function is $fp1
*/
fclose($fp1);
/*
Everyone knows how to use the echo function, which is equivalent to print(); The meaning of output on a web page
Connect strings and variables Use "." between spaces so that you can print variables in php. The use of the connection symbol "." is very important. To be clear, please refer to the PHP online manual of this site http://www.ipp.org/manual/language The part about string concatenation in .operators.string.html
.
*/
echo "You are the ".$num." guest";
/ *
Use fopen to open the file. The opening method is "w". The file opened with the w method is in write-only mode. If the file does not exist, the file will be created. If the file exists, the file content will be cleared first.Then write, the function of this sentence is to open the file, and then pass the handle to $fp2.
*/
$fp2=fopen($datafile,"w");
/*Browse Increase the number by one*/
$num=$num+1;
/*Write the number after increasing by one to the file. The method of using fputs is very simple. The first parameter is the file handle, and the second parameter is the file handle. Each parameter is the content to be written. If you have any questions about how to use the fputs function, please refer to http://www.ipp.org/manual/function.fputs.html*/
fputs($fp2,"$num");
/*Close the open file*/
fclose($fp2);
?>
(Source: www.ipp.org)