I have the following PHP code:
$haystack = file("dictionary.txt"); $needle = 'john'; $flipped_haystack = array_flip($haystack); if (isset($flipped_haystack[$needle])) { echo "Yes it there!"; } else { echo "No, it not there!"; }
The contents of dictionary.txt are as follows (UTF-8 encoding):
john
For some reason, I keep making mistakes, even though $haystack prints without problems. Itβs just a lie that I keep getting, that keeps giving me problems. As an alternative, I tried changing $haystack to the following code, which in turn will correctly return as true:
$haystack = array("john");
Why is my code mistakenly returning false?
source share