PHP中file() 函数和file_get_contents() 函数的区别
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!
大家清楚了吧
?
?
?
轉載于:https://www.cnblogs.com/xzswwp/archive/2012/11/16/2773532.html
總結
以上是生活随笔為你收集整理的PHP中file() 函数和file_get_contents() 函数的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: webstorm修改主题色和配色
- 下一篇: socket调试工具、socket调试软