PHP store filename to variable and count number of lines in file

藏色散人
Release: 2023-04-10 14:26:02
Original
3930 people have browsed it

Hello, today’s article is very suitable for novices to practice! As we can see from the title, there are two small requirements that we need to use PHP to implement. The first requires us to store a text file name into a variable, and the second requires us to calculate the text it holds. The number of lines, I wonder if anyone has any ideas for implementation? Below we will give you a detailed introduction!

If you have read some of the articles I have written before, you all know that I will use a question form to introduce the topic of the article, so it will be the same today!

Assume the question is "How to write a PHP script to count the number of lines in a file. Note that you need to store a text file name into a variable and count the number of text lines it has."

Then let’s write a solution to this problem:

First we create a PHP sample file, which I named here demo.php;

Then enter the code directly as follows:

<?php

$file = basename($_SERVER[&#39;PHP_SELF&#39;]);
$no_of_lines = count(file($file));
echo " $file 中有 $no_of_lines 行"."<br>";
Copy after login

Then execute the file, the result is as follows:

PHP store filename to variable and count number of lines in file

Whether the file has 5 lines, we can see the following

PHP store filename to variable and count number of lines in file

Nothing wrong!

Storing a text file name into a variable is achieved through the "basename($_SERVER['PHP_SELF']);" method;

Calculate what it has The number of text lines is achieved through the "count(file($file));" method.

The basename() function is used to return the file name part of the path;

$_SERVER['PHP_SELF'] represents the current php file The location address relative to the website root directory, related to the document root;

file() function reads the entire file into an array. Similar to file_get_contents(), except that file() returns the file as an array. Each unit in the array is a corresponding line in the file, including newlines; the

count() function is used to return the number of elements in the array.

Finally, I would like to recommend the latest and most comprehensive "PHP Video Tutorial"~ Come and learn!

The above is the detailed content of PHP store filename to variable and count number of lines in file. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!