PHP is very powerful, and you can use it to do almost everything. Without complex variables and code, you can quickly make beautiful counters. Next, let's do it step by step. Actually, the principle of counters is that PHP is very powerful, and you can use it to do almost everything. Without complex variables and code, you can quickly make beautiful counters. Next, let's do it step by step.
Actually, the principle of the counter is that, first, a record file is closed, such as counter.txt or counter. log. each time you visit this page, add 1 to the file and then display the result. Therefore, our PHP code should start like this:
$ Countfile = 'JS/counter. inf ';
// Define the file written into the counter in the JS directory under the current directory // counter. inf, and then we should test whether the file can be opened
If ($ fp = fopen ($ countfile, 'r') = false) {// open the file in read/write mode. if the file cannot be opened, exit.
Printf ('open file % s failed! ', $ Countfile );
Exit;
}
Else
{
// If the file can be opened normally and the data in the file is read, assume that it is 1
$ Count = fread ($ fp, 10 );
// Read 10-bit data
$ Count = $ count 1;
// Count
Fclose ($ fp );
// Close the current file
$ Fp = fopen ($ countfile, 'w ')
// Open the file in the shadow mode
Fwrite ($ fp, $ count );
// Write the new data after adding 1
Fclose ($ fp );
// Close the file
}
At this time, all the counting work is complete. if it is just a simple text counting, the value of the variable $ count can be output here. The following code converts $ count to image mode:
$ Fp = fopen ($ countfile, 'r'); // open the file in read-only mode
$ Array_count = 1; // defines a variable that represents the position of an array element.
While (! Feof ($ fp )){
$ Current_number = fgetc ($ fp );
$ Counter_array [$ array_count] = $ current_number;
$ Array_elements = count ($ counter_array );
$ Array_count = $ array_count 1;
}
The above while loop is used to separate each digit. It reads values from counter. inf from left to right, and writes them into an array called counter_array in sequence. The index of this array starts from 1 ($ array_count ). Assume that counter. if the number in inf is 158, the array $ counter_array [] is like this: $ counter_array [1] = 1, $ counter_array [2] = 5, $ counter_array [3] = 8. With the above work, we can easily display different digital images. the code is as follows:
Echo ('
Welcome '); For ($ array_id = 1; $ array_id <$ array_elements; $ array_id ){ Echo (''); } Echo ('guest |
');