2016-11-23 19 views

回答

8

您可以通過PHP的array_sum功能做到這一點。

$input = '1,2,3,4,5'; 
$res = explode(',',$input); 
$result = array_sum($res); 
echo $result; 

WORKING DEMO

1

使用下面的代碼:

<?php 

$input = '1,2,3,4,5'; 
$res = explode(',',$input); 
foreach($res as $row) 
{ 
$result += $row; 
} 
echo "Total :". $result; 

//OR 

echo array_sum($res); 

輸出

Total: 15 

演示:Click Here

0

首先,你需要更換,,然後簡單地循環播放等作爲

$input = str_replace(",","",'1,2,3,4,5'); 
$value = 0; 
$i = 0; 
while(isset($input[$i])) 
{ 
    $value += $input[$i]; 
    $i++; 
} 
echo $value;