Php 将数字相加作为if语句的结果

Php 将数字相加作为if语句的结果,php,string,if-statement,Php,String,If Statement,因此,我希望通过if语句将数字添加到字符串中。 例如: a = 2; b = 1; if($a == 2) { $total + 1; } if($b == 1) { $total + 1; } echo $total; 显然,这些不是我的if语句,但如果它们能够工作,那么我希望$total=2?这似乎有点简单,但我无法解决它?是的正如你所说,这很简单 $a = 2; // use $ to initialize $b = 1; $total =0; // define

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

a = 2;
b = 1;

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

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

echo $total;

显然,这些不是我的if语句,但如果它们能够工作,那么我希望$total=2?这似乎有点简单,但我无法解决它?

是的正如你所说,这很简单

$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

OP说,他想将数字添加到字符串中。所以,这不符合OP的要求。@Geshode:他说
我想要$total=2
,在第一句话中他写道
我想在字符串中添加数字
。@Geshode:是的,你是对的,但他没有在这里使用任何字符串?让我们等待他的评论?是的,所以我希望这是动态的,因为这些是if语句,我只想添加到数字中,如果if语句被触发,如果有意义的话。很抱歉不清楚,但我认为这应该可以,我将把它添加到我的代码中并运行它,谢谢@C2486的帮助。那么,你想通过添加一个整数来增加字符串的值吗?然后必须首先将字符串更改为整数。