array_sum以字符串形式返回值的总和

This seems like it should be really straightforward, but I keep getting unexpected output. I'm trying to access specified rows in a SQL database which each contain a numerical value and then calculate the sum of those values. PHP is concatenating the values as if they were strings even after I've set the datatype of the values to float. My code:

$query = "SELECT * FROM populations WHERE username ='{$_SESSION[name]}' AND region_name = 'region'"
$query .= "AND city_name = 'city'"; 
$result = mysqli_query($connection, $query);

while($row = mysqli_fetch_assoc($result)) {
$population_value = $row['population_value'];

$population_value = is_array($population_value) ? $population_value : array($population_value);

foreach($population_value as $value){
echo $value;
}       

echo array_sum($population_value);
}

I have also tried:

$total = array("");

foreach($population_value as $value){
floatval($value);
array_push($total, $value);
echo $value;
}

echo array_sum($total);

My output is always something like: 100002000030000 with 10,000 20,000 and 30,000 being the values of each population.

I've successfully calculated sums using foreach with values that weren't retrieved from MySQL. What is going on here?

$query = "SELECT * FROM populations WHERE username ='{$_SESSION[name]}' AND region_name = 'region'"
$query .= "AND city_name = 'city'"; 
$result = mysqli_query($connection, $query);

while($row = mysqli_fetch_assoc($result)) {
$population_value = $row['population_value']; 

//This is actually rewriting the array or NOT adding the value to it.
$population_value = is_array($population_value) ? $population_value : array($population_value);

//ok, so you're going to repeatedly output this?
foreach($population_value as $value){
echo $value; 
}       

echo array_sum($population_value);
}

I think what you want is this:

$query = "SELECT * FROM populations WHERE username ='{$_SESSION[name]}' AND region_name = 'region'"
$query .= "AND city_name = 'city'"; 
$result = mysqli_query($connection, $query);

$population_value=array(); //Initialize the array so we can just add to it.

while($row = mysqli_fetch_assoc($result)) {
    $population_value[]= intval($row['population_value']); //Just making sure we get a number. 


    echo end($population_value); //We could output the row again, or just grab the last element of the array we added.

}
//Now that the array is fully populated and we've made sure it's only numbers, we output the grand total.
echo array_sum($population_value);

First, don't initialize the array with an empty string. Do this instead:

$total = array();

or with the new style:

$total = [ ];

Second, rewrite the floatval thing like this:

array_push($total, floatval($value));

That should fix it...