Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More efficient way to set PHP variables over and over again

Tags:

php

I am curious of a better way of doing the code I have below, I find it repetitive and would like to cut down on it, any suggestions?

I was trying to do something with Variable Variables but I failed at getting that to work.

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

The goal is to set a color's code to a new color's code. So using the URL I am able to set the code for the color red to the color code of green so red's value would 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 how to set all the colors with less code?

A color's code should ONLY be updated if that color has a NEW value set in the URL using $_GET variables

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

like image 568
JasonDavis Avatar asked Feb 16 '23 16:02

JasonDavis


2 Answers

If I were you, I would put the assignments in the 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);
}

It's quite neat, but I'm not sure whether it works with your real code or not.

Edit I updated the code because I realized that the array $color_names was unnecessary, you have them in the keys of $colorsArray.

Edit Updated the code again because the if in the loop was unnecessary too.

like image 141
Inglis Baderson Avatar answered Feb 18 '23 10:02

Inglis Baderson


You can access all global variables by using the super global $GLOBALS. So you could do this:

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

More info about $GLOBALS.

like image 27
Jared Avatar answered Feb 18 '23 12:02

Jared