PHP Optimization of a very long switch statement

Please see below code

function GetAreaName($AreaCode) { switch ($AreaCode) { case 201: return 'New Jersey'; case 202: return 'Washington'; // this goes on till case 999: return ''; } } 

Let's say if AreaCode is 998, then it will have to go through so many cases! How can we optimize this feature? ( No , using databases.)

Am I going to build an array and do a binary search on it? But does this mean that every time a function is called an array, will it be restored? How do we collect an array once, cache it and reuse it every time this function is called?

+4
source share
4 answers

Why not just use a hash table?

 class Area { private $areaCodes = array( 201 => 'New Jersey', 202 => 'Washington', // this goes on till 999 => ''; ); function getStateByAreaCode ($areaCode) { if (array_key_exists($areaCode, $this->areaCodes)) { return $this->areaCodes[$areaCode]; } else { return false; } } } 

Name it as follows:

 $area = new Area(); $city = $area->getStateByAreaCode(303); 

Just save your class in a file and include it when you need it.

You asked how to prevent each request from creating an array: By putting this in a class, you at least keep it clean. Technically, every request is still created, but if your array is not huge (WAY is larger than area codes in the USA), it should not cause performance problems. If you are worried about creating an array every time you have a request, look at a code optimizer such as APC or Zend Optimizer. This essentially takes the byte code that PHP generates at runtime and caches it.

+7
source

It looks like you just need to save it in your database.

But if you cannot do this, draw it into some configuration file and save it in some kind of permanent object or just use a static variable:

 function foo($key) { static $cache = array(1 => 'abc', 2 => 'def', 3 => 'ghi'); if (array_key_exists($key, $cache)) { return $cache[$key]; } else { //Somehow signal an error (throw an exception, return boolean false, or something) } } 

In the above example, $cache will exist only once. (If you knew that the values ​​would never be null , you can use isset instead of array_key_exists .)

This is not very flexible, though, because changing the data requires changing the code. Usually you want your data and your code to be separate from each other.

This may mean saving it in some kind of file (json, xml, php, whatever) and loading it into some structure that you create only once. You will then pass this object or array to where it was needed. (Or, if you want to be hacked, you can use a static class. I suggest against this, though.)

+3
source

The switching condition is evaluated only once:

In a switch statement, a condition is evaluated only once, and the result is compared with each case statement. In the elseif expression, the condition is evaluated again. If your state is more complicated than a simple comparison and / or is in a closed loop, the switch may be faster. ➫➫➫

No optimization needed. However, read:
In PHP, which is faster, a large Switch statement or Array key lookup

If you want to create a configuration file, you can consider something like:

 $areas = array ( 1 => 'abc', 2 => 'def', .. ); 

Then just compare:

 if (!isset($areas[$some_code])) { // do something } else { // ok } 
+2
source

Try under pseudo code

 $areas = array('201' => 'New Jersey', '202' => 'Washington', ...... ........ '999' => ''); function GetAreaName($AreaCode) { if(isset($areas[$AreaCode])) { return $areas[$AreaCode]; } else { // do something } } 
0
source

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


All Articles