How to read a file content line by line using PHP code

Suppose we have file named file.txt we have read a file content line by line using PHP , here our objective is to retrieve a file data line by line using PHP.we know we have most famous php function fopen(“File name”,”r”),it will read entire content from the files and store it in the defined variable
see example

 $f_data = fopen("file.txt","r");
 

Now our question how to read line by line using PHP
Now we know the entire data is their in $f_data,next we have to read line by line using PHP , with the help of while loop we can do that ,for line by line read we use one more function called feof($f_data) which find out the end of the file see full example

 
 $f_data = fopen("file.txt","r");
  
  while(! feof($f_data))  {
	$result = fgets($f_data);
	echo $result."
"; echo "Good"."
"; } fclose($f_data);

e.g
file.txt

Sunday is weekend day
Monday is weekstart day

Out Put

Sunday is weekend day
Good
Monday is weekstart day
Good

How to find length of array using php

we are luck enough to find the length of an array using php, we have two build in function to find the count of an array using php.Lets see in details that function

1) sizeof() function

2) count() function

First let we check out the syntax of sizeof() function

 sizeof($Array);
example

$colors=array("RED","BLUE","GREEN");
echo sizeof($colors);

Output

3


let we check out the syntax of count() function

 count($Array);
example

$colors=array("RED","BLUE","GREEN");
echo count($colors);

Output

3

How to use html encode function in php to prevent browsers from using it as an HTML

In this section we are going check the usage of html encode in PHP here our question is how to use html encode function in php to prevent browsers from using it as an HTML .sometimes we have to display html element in the front end as it is without coveting it.for example we need to display below line

<h2>Hello</h2> <p>Hello here here we are disusing the usage of html encode function php to prevent browsers from using it as an HTML   </p>

but we know that normally it display like below

use html encode function php to prevent browsers from using it as an HTML

we are lucky enough to handle this situation we have a html encode function in php it prevent browsers from using it as an HTML lets see the function htmlentities

htmlentities(string,flags,character-set,double_encode)
htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = TRUE ]]] ) 

default
htmlentities($htmlcode);

See example how it is working

 $htmlcode='<h2>Hello</h2>
Hello here here we are disusing the usage of html encode function php to prevent browsers from using it as an HTML';
echo htmlentities($htmlcode);

Output

Hello

Hello here here we are disusing the usage of html encode function php to prevent browsers from using it as an HTML

How to apply array_slice in PHP object array

Here my requirement is to fetch first five items from an Object if it is a normal array means i can fetch the first nth item from the php array using the PHP build in function array_slice() for example we have an array like below
let we check how to get first element of array in using array_slice()
after we will discuss how to apply array_slice in PHP object array

$myarray=array("Mangoes","Jack Fruits","coconuts","Bananas","Apple","Orange");

print_r(array_slice($myarray,0,2));

Output

Array ( [0] => Mangoes [1] => Jack Fruits)

In the given example fetching first 2 values from the array using build in function array_slice();

Now my issues is here my array is not normal array it is an object array so i can’t use directly array_slice()

First i have to convert Object array to normal array and i can i apply the code see example below

$Myarray=(array)$OBJ_array;

print_r(array_slice($Myarray,0,5));

it will return an array with first five value of $OBJ_array