PHP中file() 函数和file_get_contents() 函数的区别
PHP中file() 函數(shù)和file_get_contents() 函數(shù)的作用都是將整個(gè)文件讀入某個(gè)介質(zhì),其主要區(qū)別就在于這個(gè)介質(zhì)的不同。
file() 函數(shù)是把整個(gè)文件讀入一個(gè)數(shù)組中,然后將文件作為一個(gè)數(shù)組返回。數(shù)組中的每個(gè)單元都是文件中相應(yīng)的一行,包括換行符在內(nèi)。如果失敗,則返回 false。
file_get_contents() 函數(shù)是把整個(gè)文件讀入一個(gè)字符串中。和 file() 一樣,不同的是 file_get_contents() 把文件讀入一個(gè)字符串。file_get_contents() 函數(shù)是用于將文件的內(nèi)容
讀入到一個(gè)字符串中的首選方法。
下面舉個(gè)例子,以便大家容易理解
假如你現(xiàn)在有個(gè)文件是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);
?>
在瀏覽器看對(duì)應(yīng)的效果,如下:
Array ( [0] => hello world [1] => if world is guangmen [2] => shunge ?weiping! )?
hello world if world is guangmen shunge ?weiping!
大家清楚了吧
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/xzswwp/archive/2012/11/16/2773532.html
總結(jié)
以上是生活随笔為你收集整理的PHP中file() 函数和file_get_contents() 函数的区别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: webstorm修改主题色和配色
- 下一篇: 企业级PHP后台开发框架