How to read from a file 1 line from top to the end of the file into a variable?

How to read from a file 1 line from top to the end of the file into a variable, which is then checked for a match with another variable?


Answer 1, authority 100%

I did not immediately understand the question correctly, as I understand it now – something like this:

$file = file_get_contents('file.txt');
$lines = explode("\n", $file); //  ""    
$linesTotal = count($lines);
$yourVarToTest = 'Some text example'; //    
for($i=0;$i<$linesTotal;$i++) {
    //      
    if($lines[$i] == $yourVarToTest ) {
       //    = -  
       //    :)
    }
}

Answer 2, authority 100%

In general, for large files I advise you to use

fgets();

Answer 3, authority 100%

I’m surprised that no one mentioned the file() function, which returns an array whose elements are a separate line of the file.

foreach(file('file.txt') as $key=>$val){
if($val == ''){
    echo '';
}else{
    echo ' ';
}
}

UPD:

$file = new SplFileObject('file.csv');
foreach($file as $line) {
    echo $line;
}