Note that there are some explanatory texts on larger screens.

plurals
  1. POMore efficient way to set PHP variables over and over again
    primarykey
    data
    text
    <p>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?</p> <p>I was trying to do something with Variable Variables but I failed at getting that to work.</p> <p>So basically I have a bunch of color names that I get with <code>$_GET[color-name-here']</code></p> <p>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 <code>red</code> to the color code of <code>green</code> so <code>red's value</code> would become <code>00FF00</code></p> <pre><code>// Get color and color replacement values from URL // get_value_or is ran through this code... // isset($_GET[$key]) &amp;&amp; !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' =&gt; 'FF0000', 'orange' =&gt; 'FF5000', 'yellow' =&gt; 'FFF200', 'green' =&gt; '00FF00', 'turquoise' =&gt; '00F0C8', 'blue' =&gt; '0064FF', 'purple' =&gt; '9F00FF', 'pink' =&gt; 'FF0082', 'white' =&gt; 'FFFFFF' ); // Iterate Color Array and Set New Color Values if they exist foreach($colorsArray as $colorName =&gt; $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]; } </code></pre> <p>So any ideas how to set all the colors with less code?</p> <p>A color's code should ONLY be updated if that color has a NEW value set in the URL using $_GET variables</p> <p>PS) I wasn't sure of a good title for this question, feel free to change it if you have a better one, thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload