Home > other >  I need to get dynamic dependent variable value that increases when the other variable increases in P
I need to get dynamic dependent variable value that increases when the other variable increases in P

Time:02-10

Like this when x is 1000 and y becomes 0.1 and when x is 2000 and y becomes 0.2 like that in php. Please help me if you are a good at it.you can see in here as an example

CodePudding user response:

It seems clear that one variable depends on the other.

Maybe your best course of action would be to encapsulate that logic in a class; something as follows (please keep in mind I did not work on PHP since long time ago so the syntax might be wrong):

class MyX() {
  private $value: int = 0;

  public function setValue($value: int) : void {
    $this->value = $value;
  }

  public function getY(): float {
    return round($this->value / 10000, 10);
  }
}


$x = new MyX();
$x->setValue(1000);
print($x->getY());

By leveraging magic methods you might get something easier to use. But the main idea is still to keep all logic in a single place.

CodePudding user response:

if there is a certain ratio between x and y like in your example y=(x/10000). Why you are not just doing simple math in php. $y = $x/10000 where $x is set by you and $y is set by this formulation. If the $y is already set, you can just, re-set it with the same formula which overwrites previous set value. If I understand you properly.

EDIT UPON COMMENT BELOW: Try this simple function below. Check the demo

function so_set_y($x){
    return floor($x/1000)/10;
}

DEMO

  •  Tags:  
  • Related