Home  >  Article  >  Backend Development  >  PHP counter program code example

PHP counter program code example

怪我咯
怪我咯Original
2017-07-16 10:54:261999browse

Counting is the simplest and most basic operation. A counter is a logic circuit that implements this operation. In a digital system, a counter mainly counts the number of pulses to achieve the functions of measurement, counting and control. It also has the function of measuring, counting and controlling. It has a frequency division function. The counter is composed of a basic counting unit and some control gates. The counting unit is composed of a series of various flip-flops with the function of storing information. These flip-flops include RS flip-flops, T flip-flop, D flip-flop and JK flip-flop, etc. Counters are widely used in digital systems, such as counting instruction addresses in the controller of electronic computers in order to sequentially fetch the next instruction, and recording additions and subtractions when performing multiplication and division operations in the arithmetic unit. times, such as counting pulses in digital instruments, etc. The counter can be used to display the work status of the product. Generally speaking, it is mainly used to indicate how many pieces of folding and plating work the product has completed. Its main indicator is the number of digits in the counter, the common ones are 3 digits and 4 digits. Obviously, the 3-digit counter can display up to 999, and the 4-digit counter can display up to 9999. This article mainly introduces the counter function implemented using PHP. The code is as follows

1) Text counter

<?php
$countfile="/count.txt";  //设置保存数据的文件
if (!file_exists($countfile)){//判断文件是否存在
exec( "echo 0 > $countfile");
} 
$fp = fopen($countfile,"rw"); 
$length=filesize($countfile);
$num = fgets($fp,$length); 
$num += 1; 
exec( "rm -rf $countfile");
exec( "echo $num > $countfile");
print "访问量总计:"."$num"."人次";  //显示访问次数
?>

2) Graphic counter

<?
$countfile="/count-num.txt";  //设置保存数据的文件
if (!file_exists($countfile))  //判断文件是否存在
{exec( "echo 0 > $countfile");} 
$fp = fopen($countfile,"rw"); 
$length=filesize($countfile);
$num = fgets($fp,$length); 
$num += 1; 
exec( "rm -rf $countfile");
exec( "echo $num > $countfile");
$len_str = strlen($num);
for($i=0;$i<$len_str;$i++){
$each_num = substr($num,$i,1);
$out_str = $out_str . "<img src=\"$each_num.gif\">";
}
print "访问量总计:"."$out_str"."人次";  //显示访问次数
?>

The above is the detailed content of PHP counter program code example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn