[英]Warning on in_array() for Wrong datatype for second argument, when working with strings
Im using this code to read out of a file, Im getting an error Warning on in_array() for Wrong datatype for second argument. 我正在使用此代码读取文件,我在in_array()上收到错误警告,表示第二个参数的数据类型错误。
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 $ExistingSongs
will be null
. 如果未设置
$_POST['submit']
则$ExistingSongs
将为null
。 in_array
is right to complain about that. in_array
对此抱怨是正确的。
If you were developing with all errors enabled -- done with error_reporting(E_ALL | E_STRICT)
or through php.ini
-- then you 'd also have gotten a notice about $ExistingSongs
being an undefined variable. 如果您在启用所有错误的情况下进行开发
error_reporting(E_ALL | E_STRICT)
通过error_reporting(E_ALL | E_STRICT)
或通过php.ini
error_reporting(E_ALL | E_STRICT)
,那么您还将收到有关$ExistingSongs
是未定义变量的通知。
如果未设置$_POST['submit']
变量,则$ExistingSongs
变量将保持未定义状态,这可能会引起警告。
stripslashes expects a string as input, and outputs a string: http://php.net/stripslashes stripslashes需要一个字符串作为输入,并输出一个字符串: http : //php.net/stripslashes
$SongToAdd is a string when you call in_array()
, hence your error. $ SongToAdd是调用
in_array()
时的字符串,因此会出错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.