$n = 10;
$var1 = 1;
$var3 = 1;
$var5 = 1;
$sum = 0;
for ($i=1; $i <= $n; $i++){
if (isset(${"var$i"}))
{
$sum += ${"var$i"};
}
}
echo $sum; // 3
$vars = [];
$zwvola = 1;
$wuvola = 1;
$zwvola = 1; // дубликат
$duvola = 1;
$wdvola = 1;
foreach (get_defined_vars() as $name => $value)
{
if (is_int($value)){
$vars[$name] = $value;
}
}
echo array_sum($vars); // 4