问题
I have this right now to use a cookie value if exists otherwise use a default value:
$default_carat_min = "0.25";
if($_COOKIE["diamond-search_caratMin"])
{
$default_carat_min = $_COOKIE["diamond-search_caratMin"];
}
I am going to have to do this with a lot of variables, and its going to get really cluttered/ugly. So I am trying to come up with a cleaner way of writing this.
I tried:
$default_carat_min = $_COOKIE["diamond-search_caratMin"] | "0.25";
Which did not work.
I can do this:
$default_carat_min = $_COOKIE["diamond-search_caratMin"] ? $_COOKIE["diamond-search_caratMin"] : "0.25";
But I don't like how I have to repeat the $_COOKIE
twice. I am wondering if there is a way to write it something like my 2nd example?
回答1:
PHP 5.3 added a shortform for the ternary operator:
$default_carat_min = $_COOKIE["diamond-search_caratMin"] ?: "0.25";
Which evaluates to the left side if the left side is true, and evaluates to the right side otherwise.
Prior to 5.3, however, you'd have to use the long form.
回答2:
You can use a function :
function set_default(&$var, $default) {
return isset($var) ? $var : $default;
}
$default_carat_min = set_default($_COOKIE["diamond-search_caratMin"], "0.25");
回答3:
I think the question is subjective. I personally think it's better to be verbose and there's nothing wrong with your first form because it is completely obvious what your code does.
It's not like you're limited on the number of lines or size you can use. Are you really saving that much by saving a few key strokes?
If it truly a problem, perhaps trying to reduce the number of variables you're using in the first place would be a better solution
回答4:
I agree with Cfreak's answer. I'd rather the code be "obvious".
To add to that though you don't want to have to search your code for every instance of 0.25
(or other values) so i recommend creating a config file if you don't have one and adding this...
DEFINE( 'DEFAULT_CARAT_MIN', 0.25 );
// other defaults
Then include the config file and
if($_COOKIE["diamond-search_caratMin"])
{
$default_carat_min = $_COOKIE["diamond-search_caratMin"];
}
else {
$default_carat_min = DEFAULT_CARAT_MIN;
}
you could also use the ternary operator
$default_carat_min = $_COOKIE["diamond-search_caratMin"] ? $_COOKIE["diamond-search_caratMin"] : DEFAULT_CARAT_MIN;
回答5:
To avoid the E-NOTICE use:
$default_carat_min = @$_COOKIE["diamond-search_caratMin"] ?: "0.25";
Note the use of @
to silence the error message.
回答6:
The question contained a hint that quite a lot of variables had to be checked to make sure they had a default value and nobody referred to that. So that's what i'm going to do now:
You can define an array of defaults first and conveniently loop through that. This example just checks the $_COOKIE global:
$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're planning to set defaults for multiple variables and different types you can go with 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;
}
}
}
Next step would be to produce the $defaults array by parsing some ini-file so you can easily centralize your defaults in a readable and easy-editable way. I'm not going to show that here though as i think it's beyond of what was asked here.
Hope someone likes this ....
来源:https://stackoverflow.com/questions/3761910/shortest-way-to-assign-a-default-value-to-a-variable