Use a PHP program to directly call the text file content instance

Source: Internet
Author: User
How to use php (as the mainstream development language) to directly call text file content: first, use the file function to open a text file on the server and return; myFile will be the handle of the file operation, and then take out the content of each row in the file and print it out through the loop command. SyntaxHighlighter. al

How to use php (as the mainstream development language) to directly call text file content: first, use the file function to open a text file on the server, the returned $ myFile will be the handle of the file operation, and then the content of each row in the file will be taken out and printed through the loop command.

Programming Logic

The first function to be used is fi1e. This function is similar to readfile (). in different places, the file function reads all the content in the file, and output to the array variables, each row is a separate array element. Use the file function to open a data.txt file. the content of the data.txt file is:

First Line 1

Row 2

Row 3

The statement for opening a file is $ myFile = file ("data.txt"). $ myFile is an array variable.

Then print the content of each element of the array through the loop command loop in the array. here the count () function is used

Obtains the number of array elements. Print ($ myFile [$ index]. "").

Creation steps

1. use the file function to open a file
The following is a reference clip:
$ MyFile = file ("data.txt ");

2. loop through the array and print the elements of the array

Here

The following is a reference clip:
For ($ index = 0; $ index

For ($ index = 0; $ index {
Print ($ myFile [$ index]. "<br> ");
}


At the same time, the elements in the array are printed out.

3. program code

The following is a reference clip:


Call text file content


// Print each line while opening the file
$ MyFile = file ("data.txt ");
For ($ index = 0; $ index {
Print ($ myFile & 91; $ index & 93 ;."
");
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.