Please, can anyone help? such in php array, “d” is double the previous element of the array and return the sum of the array

For example $input=[2,3,4,$d]; $output=17
$input=[3,1,$d,6]; $output=12
writing a function for it
I tried this. It is not working

<?php
function sum($input){
    $output=0;
    for($i=0; $i<count($input); $i++){
        $output+=$input[$i];
    }
    return $output;
}
echo sum($input);
?>