-3

This is what i have as my code currently:

<?php
    
    foreach ($data_items as $d)
    {
        echo $d['x']." + ";
    }
    
    echo " = " .$sum;
?>

It is printing out as

2 + 3 + 4 + = 9

I need it to print without the last "+" sign. Ex:

2 + 3 + 4 = 9

Thanks for the help :) .:Alter

Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42
Alter
  • 1

1 Answers1

0
<?php
$i = 0;
$len = count($data_items);
    
foreach ($data_items as $d)
{
    if ($i == $len - 1) {
        echo $d['x'];
    }else{
        echo $d['x']." + ";
    }
    $i++;
}
    
    echo " = " .$sum;
?>
Pasalar
  • 21
  • 5
  • 2
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Dec 04 '20 at 20:01
  • thank you for information, i don't do again. – Pasalar Jan 15 '21 at 08:51