您的位置:首页 > 编程语言 > PHP开发

PHP中file() 函数和file_get_contents() 函数的区别

2012-11-16 16:40 176 查看
PHP中file() 函数和file_get_contents() 函数的作用都是将整个文件读入某个介质,其主要区别就在于这个介质的不同。

file() 函数是把整个文件读入一个数组中,然后将文件作为一个数组返回。数组中的每个单元都是文件中相应的一行,包括换行符在内。如果失败,则返回 false。

file_get_contents() 函数是把整个文件读入一个字符串中。和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。file_get_contents() 函数是用于将文件的内容

读入到一个字符串中的首选方法。

下面举个例子,以便大家容易理解

假如你现在有个文件是a.txt 如下

hello world
if world is guangmen
shunge ?weiping!

里面那是换行

在file.php里面写入一下代码:

<?php

$arr=file("a.txt");
print_r($arr);
echo "<br/>";
$a=file_get_contents("a.txt");
print_r($a);

?>

在浏览器看对应的效果,如下:

Array ( [0] => hello world [1] => if world is guangmen [2] => shunge ?weiping! )
hello world if world is guangmen shunge ?weiping!

大家清楚了吧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: