通过if语句将数字相加

时间:2018-08-01 01:30:46

标签: php string if-statement

因此,我希望通过if语句将数字添加到字符串中。 例如:

a = 2;
b = 1;

if($a == 2) {
    $total + 1;
}

if($b == 1) {
    $total + 1;
}

echo $total;

显然,这些不是我的if语句,但是如果它们起作用,那么我希望$ total等于2?这似乎很简单,但我无法解决?

1 个答案:

答案 0 :(得分:1)

,这很简单。

$a = 2; // use $ to initialize 
$b = 1;
$total =0; // define a total var to take changes 
  if($a == 2) {
     $total += 1;// asign changes to total with = sign
    }
     if($b == 1) {
     $total += 1;// asign changes to total with = sign
    }
    echo $total;// will give you 2

Live demo