在php中,如果用户在数组中,则写入日志
有人能解释一下为什么下面的方法不起作用吗 如果在php中,如果用户在数组中,则写入日志,php,arrays,foreach,Php,Arrays,Foreach,有人能解释一下为什么下面的方法不起作用吗 如果$user在数组$blockedusers $blockedusers = array("USER1", "USER2"); $user = "USER1"; foreach ($user as $blockedusers) { $file = 'log.txt'; $current = file_get_contents($file); $current .= 'Blocked user' . "\n"; file_
$user
在数组$blockedusers
$blockedusers = array("USER1", "USER2");
$user = "USER1";
foreach ($user as $blockedusers) {
$file = 'log.txt';
$current = file_get_contents($file);
$current .= 'Blocked user' . "\n";
file_put_contents($file, $current);
}
有什么想法吗?如果您只想检查某个特定用户是否在
$blockedusers
数组中,则不需要循环。有一个内置函数用于此目的,建议使用它
使用:
或者,如果您有一组用户,并且您想检查他们中是否有人在阻止列表中,您可以执行以下操作:
$users = array('foo', 'bar', 'baz');
foreach ($users as $user) {
if (in_array($user, $blockedusers)) {
$current = file_get_contents($file);
$current .= 'Blocked user' . "\n";
file_put_contents($file, $current);
}
}
另外,
ini_集('display_errors','1');错误报告(E_全部)代码>避免空白页。我的@fedorqui不好,但它仍然没有达到我想要的效果:/n您使用的foreach()
错误。尝试在_array()中使用。
。这就是方法!:)
$users = array('foo', 'bar', 'baz');
foreach ($users as $user) {
if (in_array($user, $blockedusers)) {
$current = file_get_contents($file);
$current .= 'Blocked user' . "\n";
file_put_contents($file, $current);
}
}