请帮助完成此功能我想在此计算$ answer的总值。
<?php
foreach($_POST as $fieldName=> $answer){
if($answer=="1"){settype($answer, "integer");
if(is_int($answer)){
//I want to echo the total value of $answer. Right now it gives me "1111" instead of "4" and I want 4.
}
?>
答案 0 :(得分:0)
$new_answer = 0;
foreach($_POST as $fieldName=> $answer)
{
if($answer=="1"){settype($answer, "integer");
if(is_int($answer)){
$new_answer = $new_answer + $answer
}
echo $new_answer;
答案 1 :(得分:0)
PHP语言是multitype,因此您只需检查值即可简化代码:
$total = 0;
foreach($_POST as $fieldName => $answer)
{
( $answer == (int) $answer ) && $total+= $answer;
}
我的问题是否正确?
答案 2 :(得分:0)
你可以试试这个,
<?php $myTotal = 0;
foreach($_POST as $fieldName=> $answer){
if($answer=="1"){settype($answer, "integer"); }
if(is_int($answer)){
$myTotal +=$answer;
}
}
?>