Home >Backend Development >PHP Problem >How to convert php files into strings

How to convert php files into strings

青灯夜游
青灯夜游Original
2021-09-23 15:56:454186browse

The file_get_contents() function can be used in php to convert the file into a string. It can read the contents of the file into a string. The syntax is "file_get_contents(file,include_path,context,start,maxlen) ".

How to convert php files into strings

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In PHP, you can use the file_get_contents() function to convert the file into a string.

The file_get_contents() function can read the contents of the file into a string. The syntax format of the function is as follows:

file_get_contents(file,include_path,context,start,maxlen)

The parameter description is as follows:

  • file : The file to be read;
  • include_path: Optional parameter, used to set whether you want to search for the file in include_path, the default is FALSE;
  • context: Optional parameter, used To represent a valid context resource created using the stream_context_create() function. If you do not need to customize the context, you can use NULL to ignore it;
  • start: Optional parameter, used to set the starting position of reading in the file . Note that this parameter cannot be used for remote files;
  • maxlen: Optional parameter, used to set the number of bytes to be read. The default is to read the entire content of the file.

Note: When the file_get_contents() function fails to execute, it may return FALSE of Boolean type, or it may return a non-Boolean value (such as a null character). Therefore, the === operator is generally used to test the return value of this function.

Example:

There is such a text file, the content is:

How to convert php files into strings

Use the file_get_contents() function to convert the file into a string

<?php
header("Content-type:text/html;charset=utf-8");
$file = &#39;test.txt&#39;;
$filestr = file_get_contents($file);
if ($filestr) {
	echo $filestr;
} else {
	echo &#39;失败!&#39;;
}
?>

Output result:

How to convert php files into strings

Recommended learning: php training

The above is the detailed content of How to convert php files into strings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn