what the PHP function to add numbers together? Like:
echo function("43534");
The output will be: 19 (4+3+5+3+4)
what the PHP function to add numbers together? Like:
echo function("43534");
The output will be: 19 (4+3+5+3+4)
There is not internal PHP function for this. You can do this using str_split and array_sum methods.
function add($string)
{
return array_sum(str_split($string));
}
And call:
echo add("43534");
Of curse you need to do some validations.
You need a very basic algorithm for this:
Here is your function:
<?php
function add_numbers($string){
$result = 0;
$num_array = explode("+", $string);
foreach($num_array as $num_array_element){
$result += intval($num_array_element);
}
return $result;
}
echo add_numbers("4+3+5+3+4"); // this prints 19
?>