When you have a single =
, you are performing an assignment on that variable. And that expression ends up being the value you stored to the variable. So, if ($contents = 10)
is basically the same as if (10)
(which is always "true" in PHP)
You need to use a comparison operator, such as ==
to compare the value of $contents
with the value you are looking for. (ie. 10)
Also, the function file
returns an array, with each value of that array corresponding to 1 line of the file. So, you either need to use file_get_contents
to return everything as a single string, or reference the particular line of that file as the index of the array.
// Using file()
$contents = file("textdocument.txt");
if ($contents[0] == 10) {
// If the contents of the first line of the file is '10', do something
}
// Using file_get_contents()
$contents = file_get_contents("textdocument.txt");
if ($contents == 10) {
// If the contents of the entire file is '10', do something
}