My current code:
$file = fopen(\"countries.txt\",\"r\");
$array = array();
while(!feof($file)) {
$array[] = fgets($file);
}
fclose($file);
When you compare strings - you should always use '==='.
The problem is, that you have a new line character at the end of each line, so:
test\n !== test
//^^ See here
That's why it doesn't work as you expect it to.
How to solve it now? Can I introduce to you the function: file(). You can read a file into an array and set the flag to ignore these new lines at the end of each line.
So putting all this information together you will get this code:
$array = file("countries.txt", FILE_IGNORE_NEW_LINES);
$str = "test";
foreach ($array as $key => $val) {
if ($val == $str) {
echo $val;
} else {
echo "not found";
}
}