今天学习高级部分

先从简单的开始,时间和日期:

1
2
3
<?php
echo "Time is".date("h, i, sa");
?>

解释一下,

1
2
3
4
h: hour
i: minute
s: second
a:小写的am,pm后缀

image-20220228225616498

显示如图所示。

在网站上可以自动更新版权年份:

1
2
3
4
5
<html>
<body>
&copy-<?php echo date("Y");?>
</body>
</html>

image-20220228225818425

如图所示。

最简单的年月日表示:

1
2
3
4
5
<?php
echo "Today is ".date("Y-m-d")."<br>";
echo "Today is ".date("Y.m.d")."<br>";#此处表示年月日
echo "Today is ".date("l")."<br>";#此处表示星期几
?>

再来介绍一个新函数:

1
strtotime();#用来我们自己设定一个时间,下面的date函数执行我们这个时间
1
2
3
4
5
6
7
<?php
$d = strtotime("tomorrow");
echo date("Y-m-d h:i:sa", $d)."<br>";

$d = strtotime("next Saturday");
echo date("Y-m-d h:i:sa", $d)."<br>";#此处注意用法!两个参数!
?>

image-20220228230859744

下面正式开始PHP高级部分:

include和require

他们其实是差不多的,只不过如果require执行错误会结束整个脚本的执行。

如果include执行错误只会在它这里跳过,不影响下面语句的执行。

举例子,先写一个php文件

1
2
3
<?php
echo "<p>Copyright &copy; 1999-".date("Y")."W3schools.com</p>";
?>

也就是一个版权文件

下面用include将其展示在html前端页面上。

1
2
3
4
5
6
7
8
9
<html>
<body>

<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include "footer.php";?>
</body>
</html>

image-20220228231442599

再来一个例子:

1
2
3
4
5
#先写一个PHP文件
<?php
$color = 'red';
$car = "BMW";
?>

再写前端

1
2
3
4
5
6
7
8
9
<html>
<body>
<h1>Welcome to my home page</h1>
<?php include "vars.php";
echo "I have a $color $car.";
?>

</body>
</html>

下面看require的例子:

1
2
3
4
5
6
7
8
9
10
<html>
<body>

<h1>Welcome to my home page!</h1>
<?php require 'noFileExists.php';
echo "I have a $color $car.";
?>

</body>
</html>

执行结果应该是找不到文件以及之后的部分都不能显示了。如图:

image-20220228232637144

只能到这里。

下面开始读取文件:

1
readfile()#读取文件并将其写到缓冲区。

我们写一个txt文件

1
2
3
4
5
6
7
AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language

然后读取:

1
2
3
<?php
echo readfile("webdictionary.txt");
?>

image-20220228233418232

读取结果!

1
fopen()比readfile()提供了更多的选择。
1
fopen()第一个参数表示打开哪个文件,第二个参数表示哪种方式打开。下面是第二个参数的图。
Modes Description
r Open a file for read only. File pointer starts at the beginning of the file
w Open a file for write only. Erases the contents of the file or creates a new file if it doesn’t exist. File pointer starts at the beginning of the file
a Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn’t exist
x Creates a new file for write only. Returns FALSE and an error if file already exists
r+ Open a file for read/write. File pointer starts at the beginning of the file
w+ Open a file for read/write. Erases the contents of the file or creates a new file if it doesn’t exist. File pointer starts at the beginning of the file
a+ Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn’t exist
x+ Creates a new file for read/write. Returns FALSE and an error if file already exists
1
fread()是从打开的文件中读取数据。第一个参数是文件名,第二个参数是读取的二最大字节数!也就是限制了读取数量

​ 因为一个文件被fopen打开后,服务器不希望它一直打开着占内存。所以我们最好在代码中关闭它。

1
2
3
4
<?php
$myfile = fopen("webdictionary.txt","r");
fclose($myfile);
?>

下面看fgets()函数,用于从文件中读取一行。

1
2
3
4
5
<?php
$myfile = fopen("abc.txt", "r") or die("unable to open file!");
echo fgets($myfile);#注意用法!
fclose($myfile);
?>

调用fgets()后,文件指针指向文件下一行。

下面看检查文件结束符—feof()

对于遍历未知长度的数据非常有用。

1
2
3
4
5
6
7
8
<?php
$myfile = fopen("abc.txt", "r") or die("Ubable to open file!");

while(!feof($myfile)) {#此处检查文件指针是否在末尾!
echo fgets($myfile)."<br>";#一行一行输出直到最后一行!
}
fclose($myfile);
?>

image-20220301000216573

既然有读取一行,也有读取一个字符!—fgetc()

1
2
3
4
5
6
7
8
9
<?php
$myfile = fopen("abc.txt", "r") or die("Unable to open file!");

while(!feof($myfile)) {
echo fgetc($myfile);#只要不是最后一个字符,就一个一个打出来!

}
fclose($myfile);
?>

调用fgetc后,文件指针向下一个字符移动。

image-20220301000606631

下次学写文件!