The shortest way to assign a default value to a variable?

I have this right now to use a cookie if the default value is otherwise used:

$default_carat_min = "0.25"; if($_COOKIE["diamond-search_caratMin"]) { $default_carat_min = $_COOKIE["diamond-search_caratMin"]; } 

I am going to do this with a lot of variables and it will be really cluttered / ugly. So I'm trying to come up with a cleaner way to write this.

I tried:

 $default_carat_min = $_COOKIE["diamond-search_caratMin"] | "0.25"; 

What didn't work.

I can do it:

 $default_carat_min = $_COOKIE["diamond-search_caratMin"] ? $_COOKIE["diamond-search_caratMin"] : "0.25"; 

But I do not like how I should repeat $_COOKIE twice. I am wondering if there is a way to write something like my second example?

+4
source share
6 answers

PHP 5.3 added a short form for the ternary operator :

 $default_carat_min = $_COOKIE["diamond-search_caratMin"] ?: "0.25"; 

Which evaluates the left side if the left side is true, and otherwise evaluates it on the right side.

Prior to 5.3, however, you will have to use a long form.

+20
source

You can use the function:

 function set_default(&$var, $default) { return isset($var) ? $var : $default; } $default_carat_min = set_default($_COOKIE["diamond-search_caratMin"], "0.25"); 
+4
source

I think the question is subjective. I personally think it's better to be detailed, and there is nothing wrong with your first form, because it’s very obvious what your code is doing.

It doesn't seem like you are limited by the number of rows or sizes you can use. Do you really save it by saving a few key touches?

If this is really a problem, perhaps trying to reduce the number of variables you use will be the best solution

+2
source

I agree with Cfreak's answer. I would prefer the code to be "obvious."

To add to this, although you do not want to search for your code for each instance of 0.25 (or other values), I therefore recommend creating a configuration file if you do not have one, and add this ..

 DEFINE( 'DEFAULT_CARAT_MIN', 0.25 ); // other defaults 

Then include the configuration file and

 if($_COOKIE["diamond-search_caratMin"]) { $default_carat_min = $_COOKIE["diamond-search_caratMin"]; } else { $default_carat_min = DEFAULT_CARAT_MIN; } 

you can also use ternary operator

 $default_carat_min = $_COOKIE["diamond-search_caratMin"] ? $_COOKIE["diamond-search_caratMin"] : DEFAULT_CARAT_MIN; 
+2
source

To avoid using E-NOTICE:

 $default_carat_min = @$_COOKIE["diamond-search_caratMin"] ?: "0.25"; 

Note the use of @ to disable the error message.

+1
source

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 ....

0
source

All Articles