Im using this code to read out of a file, Im getting an error Warning on in_array() for Wrong datatype for second argument.
if (isset($_POST['submit'])) {
$SongToAdd = stripslashes($_POST['SongName']) . "\n";
$ExistingSongs = array();
if (file_exists("SongOrganizer/songs.txt") && filesize("SongOrganizer/songs.txt") > 0) {
$ExistingSongs = file("SongOrganizer/songs.txt");
}
}
if (in_array($SongToAdd, $ExistingSongs)) {
echo "<p>The song you entered already exists!<br />\n";
echo "Your song was not added to the list.</p>";
The text file contains:
Bang Bang
Doctor
Hello
Ice Cream Man
Show Me
Doctor
Well, what about the first time your page loads? If
$_POST['submit']is not set then$ExistingSongswill benull.in_arrayis right to complain about that.If you were developing with all errors enabled — done with
error_reporting(E_ALL | E_STRICT)or throughphp.ini— then you ‘d also have gotten a notice about$ExistingSongsbeing an undefined variable.