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')) {
$a='$user["fivestar"]';
$b='$user["fourstar"]';
$c='$user["threestar"]';
$e='$user["twostar"]';
$f='$user["onestar"]';
$g='$user["best_answer"]';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
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){
$a=$user["fivestar"]*5;
$starrating .= '<span>'.$user["fivestar"].' x 5 Stars</span>';
}
if($user["fourstar"] > 0){
$b=$user["fourstar"]*4;
$starrating .= '<span>'.$user["fourstar"].' x 4 Stars</span>';
}
if($user["threestar"] > 0){
$c=$user["threestar"]*3;
$starrating .= '<span>'.$user["threestar"].' x 3 Stars</span>';
}
if($user["twostar"] > 0){
$d=$user["twostar"]*2;
$starrating .= '<span>'.$user["twostar"].' x 2 Stars</span>';
}
if($user["onestar"] > 0){
$e=$user["onestar"]*1;
$starrating .= '<span>'.$user["onestar"].' x 1 Stars</span>';
}
if(isset($user["best_answer"])){
$f=$user["best_answer"]*100;
$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.
- 34 reads
- 592 reads