Is there a "map" function in php?

I am trying to find the php equivalent of processing the "map" function so that I can redisplay a number from one range to another. Is there anything? Is this called something else?

http://processing.org/reference/map_.html

For example, to display a value from 0-100 to 0-9.

map(75, 0, 100, 0, 9); 
+6
source share
2 answers

There is no built-in function for this, but it is easy to create it:

 function map($value, $low1, $high1, $low2, $high2) { return ($value / ($high1 - $low1)) * ($high2 - $low2) + $low2; } 

This is not tested, but you should hope to get this idea.

+10
source

Thanks for this great feature!

I would personally improve this feature a bit by adding a simple error check to avoid dividing by 0, which would make PHP a fatal error.

 function map($value, $low1, $high1, $low2, $high2) { if ($low1 == $high1) return $low1; return ($value / ($high1 - $low1)) * ($high2 - $low2) + $low2; } 
+1
source

Source: https://habr.com/ru/post/918581/


All Articles