How do I prevent muliplicator to be zero in php

How can I prevent that a variable is cero in my math formula in php.

The result of the following formula is 0

 if (!function_exists('zusammenrechnen')) {

function zusammenrechnen($a, $b, $c, $d, $e, $f, $g) {
     $multiplikation = ((5*$a)+(4*$b)+(3*$c)+(2*$d)+(1*$e)+(100*$g));

      return $multiplikation;

echo zusammenrechnen($a, $b, $c, $d, $e, $f, $g);

I get 0 as result


Codemaster's picture

Prevent each multiplicator from being zero

You need to prevent each multiplicator to be zero. You can do this with a simple if clause:

$starrating = '';
                        if($user["fivestar"] > 0){
                            $starrating .= '<span>'.$user["fivestar"].' x 5 Stars</span>';
                        if($user["fourstar"] > 0){
                            $starrating .= '<span>'.$user["fourstar"].' x 4 Stars</span>';
                        if($user["threestar"] > 0){
                            $starrating .= '<span>'.$user["threestar"].' x 3 Stars</span>';
                        if($user["twostar"] > 0){
                            $starrating .= '<span>'.$user["twostar"].' x 2 Stars</span>';
                        if($user["onestar"] > 0){
                            $starrating .= '<span>'.$user["onestar"].' x 1 Stars</span>';
                            $starrating .= '<span>'.$user["best_answer"].' Best Answers</span>';
                        echo $starrating;
                        echo $a+$b+$c+$d+$e+$f;


I see in your code also that you declared your variables as string. Thats why the variables are returned as zero.

Vote the answer: 
No votes yet