PHP how to add integers together from for loop

You got some issues in your code :

  • you are using $values wich doesn't exists, you should replace it with $points.
  • you add points even if the character isn't in your $letters (try with "test me"), the space add points related to T.
  • your code can't handle your last letters ("ER", "CL", "IN", "TH", "QU")

You can do this without using array_sum, with += :

$tally += $points[$spot]

And to go further, your code can't handle the last $letters, you need to change your code to check these letters too.

Working code, with some var_dump :

if(isset($_POST['play'])) {
    $spot = "";
    $tally = 0;
    //GET WORD FROM USER AND UPPERCASE IT
    $ply1_word = strtoupper($_POST['player1']);
    var_dump($ply1_word);
    //TURN WORD STRING INTO ARRAY
    $word_letters = str_split($ply1_word);
    print_r($word_letters);
    $skipNextLetter = false; // needed to handle "ER", "CL", "IN", "TH", "QU"
    for ($i = 0; $i < count($word_letters); $i++)  {
        // reset $spot to prevent adding points when char isn't in $letters
        $spot = '';
        // don't check this char if old one was a combination of two chars
        if ($skipNextLetter) {
            $skipNextLetter = false;
            continue;
        }
        $char = $word_letters[$i];
        $charTemp = $word_letters[$i];
        // handle "ER", "CL", "IN", "TH", "QU"
        if (isset($word_letters[$i + 1])) {
            $charTemp .= $word_letters[$i + 1];
            var_dump('===> check with next char :: ' . $charTemp);
            if (in_array($charTemp, ['ER', 'CL', 'IN', 'TH', 'QU'])) {
                $char = $charTemp;
                $skipNextLetter = true;
            }
        }
        if (in_array($char, $letters)) {
            $spot = array_search($char, $letters);
        }
        var_dump('CHAR => ' . $char . ' / SPOT => ' . $spot);
        if (isset($points[$spot])) {
           echo $points[$spot] . "<br>";
        //    print_r($points[$spot]);
           var_dump('ADDING POINTS : ' . $points[$spot]);
//  NEED TO ADD NUMBERS TOGETHER HERE OR AFTER HERE
            $tally += $points[$spot];
        }
    }

Results :

$test = 'Stackoverflow';
// $tally = 65
$test = 'Stackoverflow Clean';
// $tally = 84

As Fritz said, I would have done things in a different way (ie. with array_values)