We often encounter fopen() and file_get_contents() in our PHP development. I believe many students find that these two functions are basically the same, so today we will talk about php fopen What is the difference between () and file_get_contents()? Enough nonsense, let’s take a look!
Reading files in phpYou can use the two functions fopen and file_get_contents. There is no essential difference between the two, but the former php codeis similar to reading files. A little more complicated than the latter. This article explains the implementation code of fopen and file_get_contents to read files through examples.
The code for fopen to read files is as follows:<?php $file_name = "1.txt"; echo $file_name . " "; $fp = fopen($file_name, 'r'); //$buffer=fgets($fp); while (!feof($fp)) { $buffer = fgets($fp); echo $buffer; } fclose($fp); ?>
<?php if (file_exists($path)) { $body = file_get_contents($path); echo $body; //输入文件内容 } else { echo "文件不存在 $path"; } ?>
Let me introduce fopen() and file_get_contents( ) The difference in usage of opening a URL to obtain web page content
In PHP, if you want to open a web page URL to obtain web page content, the more commonly used functions are fopen() and file_get_contents(). If the requirements are not strict, these two functions can be chosen arbitrarily according to personal preferences in most cases. This article will talk about the differences in the usage of these two functions, as well as the issues that need to be paid attention to when using them. fopen() opens the URLThe following is an example of using fopen() to open the URL:<?php $fh = fopen('//m.sbmmt.com/', 'r'); if($fh){ while(!feof($fh)) { echo fgets($fh); } } ?>
<?php $fh= file_get_contents('//m.sbmmt.com/'); echo $fh; ?>
Recommended related articles:
This article mainly introduces a summary of PHP's opening and closing file operation functions. This article explains fopen() and fclose()Summary of usage of PHP fopen() and fclose() functions
PHP fopen and fwrite functions create html pages. Idea: Use the fopen function and fread function to get the template...PHP fopen and fwrite functions create html pages
The above is the detailed content of The difference between php fopen() and file_get_contents() is explained in detail. For more information, please follow other related articles on the PHP Chinese website!