The question hinted that enough variables had to be checked to make sure they had a default value, and no one referred to it. So what I will do now:
First you can define an array of default values ββand conveniently run the loop. This example just checks the global $ _COOKIE:
$defaults = Array( 'diamond-search_caratMin' => "0.25" ,'diamond-search_caratMax' => "2.00" ); foreach ($defaults as $dk => $dv) { if (!isset($_COOKIE[$dk])) { $_COOKIE[$dk] = $dv; } }
If you plan to set default values ββfor several variables and different types, you can proceed with the following code:
$defaults = Array( '_COOKIE' => Array( 'diamond-search_caratMin' => "0.25" , 'diamond-search_caratMax' => "2.00" ) , 'myOtherArray' => Array( 'value_1' => 10 , 'value_2' => 20 ) , 'myString' => 'Hello' , 'myFloat' => 1.0 ); foreach ($defaults as $vk => $vv) { if (is_array($vv)) { if (!isset($$vk)) { $$vk = Array(); } foreach ($vv as $dk => $dv) { if (!isset($$vk[$dk])) { $$vk[$dk] = $dv; } } } else { if(!isset($$vk)) { $$vk=$vv; } } }
The next step is to create an array of $ defaults by parsing some ini file so that you can easily centralize your default values ββin an easy to read and easily editable form. I am not going to show it here, although I think it is higher than what was set here.
Hope someone likes it ....
source share