Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我试图找到不为空的输入框的数量,但我每次都得到 1 的值。我究竟做错了什么?
foreach( $_POST as $key=> $value ) { if ($value!='' && $key!='add') { $count = count($value); } } echo $count;
您只是将$count变量重新定义为count($value)(始终1因为$value它不是数组),而不是递增它。
$count
count($value)
1
$value
适当时使用增量运算符将 1 加到$count:
$count ++;
您也可能更好地使用empty(..)您的支票,如下所示:
empty(..)
$count = 0; foreach($_POST as $key => $value) { if($key != 'add' && !empty($value)) { $count++; } } echo $count;