Skip to content
Advertisement

I want the minimum to be 0 but the substraction is still negative

I’m beggining in OOP with PHP, and i want to make 2 opponent fight, with the function fight. I want to have a $degat variable which is a result of a radom number between 1 to strength of attacker – (minus) dexterity of defender. So the result must be of minimum 0.

I have this code and when I var_dump it I still have negative value. I can’t see what i’m doing wrong.

JavaScript

thanks !

Advertisement

Answer

Instead of storing the result of rand(1, $this->strength) - $target->dexterity) in a variable, you could also omit the if-statement completely and reduce your method to:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement