Thursday, 26 November 2015

31_php_file_handling_read_data

Reading data in File Handling


Create one PHP page and write code as written below and run the page.



Run the Page and enter the data whatever you would like to enter, and then after check the harshida_names.txt file. So this is dynamic way to enter the data in to your .txt file. So data is stored in text file but if we want to retrieve the data from the text file. Then we need to do some changes in our code and apply the foreach loop. Let’s study it through the example.



To read the data line by line basis we need to use file() function in PHP. PHP trim() function is used to remove the white space from our data as well as also special character. When you run the page you can see that last word also contain “,” in your output. So how can we remove the last “,” from our output? Again we need to change some of code.



So here we have created one variable name $counter and initialize it as value 1. Then after we are counting the file using variable $readdata_count variable. Inside the foreach() loop we have added if which check that if $count1<$readdata_count then add “,” at end otherwise not.



No comments:

Post a Comment