CodeSky 代码之空

随手记录自己的学习过程

PHP 代码行数倒计时 说说两个文件函数

2014-03-07 21:30分类: PHP评论: 0

前两天看到交大平均4年10W行,然后就跃跃欲试写了一个简单的统计,其实还是表单输入而已,想了想用数据库未免太过浪费,文件函数正好不熟,故用之。

因为很简陋,没有过滤什么的,所以不演示了,自己跑下便知:

1<!DOCTYPE html>
2<html>
3<head>
4	<title>Count Code</title>
5</head>
6<body>
710W代码量还有:
8	<?php
9		$file = fopen('count.txt', 'r');
10		$num = fgets($file);
11
12		echo number_format($num);
13		fclose($file);
14	?>
15	<br/>
16	<form action="index.php" method="post">
17		<input type="text" placeholder="输入这次的代码行数" name="line" />
18		<input type="submit" value="提交" />
19	</form>
20	<?php
21		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
22			$line = $_POST['line'];
23			$now = $num - $line;
24			file_put_contents('count.txt', $now);
25		}
26	?>
27
28</body>
29</html>
30

其中fgets()函数是读取了一行,扩展阅读:http://mx1.php.net/manual/zh/function.fgets.php

file_put_contents()效果等同于fopen()+fwrite()+fclose(),扩展阅读: http://www.w3school.com.cn/php/func_filesystem_file_put_contents.asp http://mx1.php.net/manual/zh/function.file-put-contents.php

评论 (0)