15 hours----from modifying programs to their own programs _php basics

Source: Internet
Author: User
Tags php online server memory


This article belongs to the man who was blown down by the wind and has the copyright. From www.iphp.org Forum, the network reprint please keep this information. Non-network reprint please contact me. Personal Webmaster Write things not easy, but also for me to continue to write down the courage and motivation, Please do not delete this note at the time of reprint. This is the first article, I will find time to write out the later things.


Many friends of the understanding of PHP, long-term stay in the modification of other people's programs, because can be modified so lazy in progress. Or buy a book to see, found PHP function Super Invincible Many, See the function library was stunned. I belong to the latter. This article is dedicated to lazy people who spend time learning PHP, but also want to do something for their own lazy.

Later, I took part in the school of a secondary repair class, the teacher said, the final exam to test, so, was forced, incredibly can write their own simple code.

Therefore, this article is oriented to modify the program in the HTML, but a friend who cannot modify the structure of a program has a basic program base: the ability to understand if and else. enough, or to have the same basis of the per capita. The master can walk away. I still need to look up the manual to use the commonly used functions so far. Still write the garbage code again.

All right, cut the crap, let's go to the first stage.


Can modify the program of friends, in the eyes of the students are the web master level of characters. If I start from what is a variable, it will inevitably delay your time. I'm doing text collection myself, so we can start with a detailed analysis of a part of a program. The program is simple, with only a few simple judgments, I will write the instructions of the program in the invincible detail.

Quote: If you do not understand where to see, please tell me, I change in the first time, or email me, my email address is rainboy#tom.com, please do not send spam to me, I have been spam has been very annoying
Now let's start by dissecting a simplest counter

The function of the counter is to refresh the page and increase the number by one.
Procedures are as follows

Code: [Copy to Clipboard]
$datafile = "Data.txt";
$FP 1=fopen ($datafile, "R");
$num =fgets ($fp 1,10);
$num =trim ($num);
Fclose ($fp 1);
echo "You are the first". $num. " Guests ";
$FP 2=fopen ($datafile, "w");
$num = $num +1;
Fputs ($fp 2, "$num");
Fclose ($fp 2);
?>
The following is a detailed explanation of the program code: [Copy to Clipboard]

/* PHP files are?> or <?php?> wrapped, the server can take out the contents of the label to perform.

/*
Set up data store files, because variables in PHP are stored in server memory, they disappear immediately after execution, so we need to use text to keep the number of visitors browsing, we use the data.txt, we can adjust according to the situation. Variables in PHP have a $ symbol in front of them, so Those with the $ symbol are variables. Create a data file manually, write 1 in it, and then save
*/
$datafile = "Data.txt";


/* This step is to open the file, using the fopen () function, the fopen function is the function that opens the file, which accepts two parameters, one is the file name, and the other is the open method. For example, this program uses the "R" method to open it in read-only mode. $FP 1 is a handle, what is a handle? Just like when you use your toothbrush, you use a toothbrush handle to make your teeth clean. Then $FP1 is like the toothbrush handle, which PHP uses to manipulate $FP1 handle to control data.txt this data store file. This step-by-step operation is summed up in one sentence: Use the fopen function to open the data file and pass the handle of the open file to $FP1, if you read here you still have questions about how to use the fopen function, please refer to this website PHP online manual http://www.iphp.org/manual/function.fopen.html
How to use the fopen function
*/
$FP 1=fopen ($datafile, "R");


/*
A word to summarize the use of this statement: Open file read data
Use functions: Fgets ("Handle", "read the number of bytes in the file"); The result is the purpose of reading the contents of the file by reading the handle $FP1. This sentence gets $num that is the first 10 bytes in the file.
*/
$num =fgets ($fp 1,10);


/*
The trim () function is used to remove spaces before and after a string, such as
$num = "123";
Trim ($num) is 123;
*/
$num =trim ($num);

/*
fclose function closes the file, the Fclose function requires a parameter that is $FP1
*/
Fclose ($fp 1);

/*

How to use the Echo function everyone knows that the equivalent of print (); The meaning of the output in the Web page
Use "." Between the connection string and the variable, so that you can print the variable in PHP. Join symbol "." is very important to use. To be clear, please refer to the PHP online manual of this site http://www.iphp.org/manual/language.operators.string.html
The section on string concatenation in.
*/
echo "You are the first". $num. " Guests ";


/*
Use fopen to open the file, open the method "W", the file opened for the W method is write-only, and if the file does not exist, create the file and, if it exists, empty the contents of the file. Then write, the function of this sentence is to open the file, and then pass the handle to $FP2.
*/
$FP 2=fopen ($datafile, "w");

* * Browse number increased one * *
$num = $num +1;

/* will add a later number to write to the file Fputs use method is very simple, the first parameter is a file handle, the second parameter is to write the content, if you have doubts about the use of fputs functions, please refer to the PHP online manual http://www.iphp.org/ manual/function.fputs.html*/
Fputs ($fp 2, "$num");

/* Close Open File * *
Fclose ($fp 2);
?>

(Source: www.iphp.org)

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.