A more efficient way to set PHP variables over and over again

I am curious how best to make the code that I have below, I find it repetitive and would like to shorten it, any suggestions?

I tried to do something with variable variables, but I was not able to get this to work.

So basically I have a bunch of color names that I get with $_GET[color-name-here']

The goal is to set the color code to a new color code. Therefore, using the URL, I can set the code for the color red to the color code green , so the red value will become 00FF00

 // Get color and color replacement values from URL // get_value_or is ran through this code... // isset($_GET[$key]) && !empty($_GET[$key]) ? $_GET[$key] : $default; $red = get_value_or('red', null); $orange = get_value_or('orange', null); $yellow = get_value_or('yellow', null); $green = get_value_or('green', null); $turquoise = get_value_or('turquise', null); $blue = get_value_or('blue', null); $purple = get_value_or('purple', null); $pink = get_value_or('pink', null); $white = get_value_or('white', null); // Define Default Color Name and Hexcode values $colorsArray = array( 'red' => 'FF0000', 'orange' => 'FF5000', 'yellow' => 'FFF200', 'green' => '00FF00', 'turquoise' => '00F0C8', 'blue' => '0064FF', 'purple' => '9F00FF', 'pink' => 'FF0082', 'white' => 'FFFFFF' ); // Iterate Color Array and Set New Color Values if they exist foreach($colorsArray as $colorName => $colorCode){ // Do something to set each color Name with a New color code, if that color name has a value set } // Right now I am doing it manually for each color name, all 9+ like this... //Set Reds NEW color value if(isset($red)){ $colorsArray['red'] = $colorsArray[$red]; } //Set oranges NEW color value if(isset($orange)){ $colorsArray['orange'] = $colorsArray[$orange]; } //Set yellows NEW color value if(isset($yellow)){ $colorsArray['yellow'] = $colorsArray[$yellow]; } 

So, any ideas on how to set all colors with less code?

The color code should ONLY be updated if this color has the NEW value set in the URL using the $ _GET variables

PS) I was not sure of a good title for this question, feel free to change it if you have a better one, thanks

+4
source share
2 answers

If I were you, I would put the tasks in a loop:

 $colorsArray = array( 'red' => 'FF0000', 'orange' => 'FF5000', 'yellow' => 'FFF200', 'green' => '00FF00', 'turquoise' => '00F0C8', 'blue' => '0064FF', 'purple' => '9F00FF', 'pink' => 'FF0082', 'white' => 'FFFFFF' ); foreach ($colorsArray as $colorName => $colorCode) { $colorsArray[$colorName] = get_value_or($colorName, $colorCode); } 

This is pretty neat, but I'm not sure if it works with your real code or not.

Edit I updated the code because I realized that the $color_names not needed, you have them in the $colorsArray keys.

Edit Updated the code again, because if in the loop is not needed either.

+5
source

You can access all global variables with the super-global $GLOBALS . So you can do this:

 foreach ($colorsArray as $colorName => $colorCode) { if (isset($GLOBALS[$colorName]) { $colorsArray[$colorName] = $GLOBALS[$colorName]; } } 

Learn more about $ GLOBALS.

+3
source

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


All Articles