Three php counter codes

Source: Internet
Author: User
Tags numeric
The code is as follows: Copy code
<? Php
// Counter
Function countx ($ file = "count. dat "){
If (file_exists ($ file )){
$ Fp = fopen ($ file, "r ");
$ Numx = fgets ($ fp, 10 );
Fclose ($ fp );
$ Numx ++;
// The above four lines of code can be replaced by an expression: $ numx = file_get_contents ($ file) + 1;
}
Else {
$ Numx = 1 ;}
File_put_contents ($ file, $ numx); // when the file does not exist, this function automatically creates the file and converts the parameter to a string for writing.
Echo $ numx;
/* The entire function body can be replaced by two expressions: file_exists ($ file )? File_put_contents ($ file, file_get_contents ($ file) + 1): file_put_contents ($ file, "1"); readfile ($ file );
*/
}
// Function call
Countx ();
?>

Code 2

The code is as follows: Copy code

<? Php
  
  
$ Counterfile = "balong.txt"; // number of file names for storing numeric values
  
Function displaycounter ($ counterfile ){
$ Fp = fopen ($ counterfile, "rw ");
$ Num = fgets ($ fp, 5 );
$ Num + = 1;
Print "you are the". "$ num". "guy reading the barrier counter ";
Exec ("rm-rf $ counterfile ");
Exec ("echo $ num> $ counterfile ");
  }
  
If (! File_exists ($ counterfile )){
Exec ("echo 0> $ counterfile ");
  }
  
Displaycounter ($ counterfile );
  
?>


Code 3

The code is as follows: Copy code

<? Php
  
  
$ Counterfile = "www.111cn.net.txt"; // number of file names for storing numeric values
  
Function displaycounter ($ counterfile ){
$ Fp = fopen ($ counterfile, "rw ");
$ Num = fgets ($ fp, 5 );
$ Num + = 1;
Print "you are the". "$ num". "guy reading the barrier counter ";
Exec ("rm-rf $ counterfile ");
Exec ("echo $ num> $ counterfile ");
  }
  
If (! File_exists ($ counterfile )){
Exec ("echo 0> $ counterfile ");
  }
  
Displaycounter ($ counterfile );
  
?>

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.