views:

62

answers:

4

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?

+3  A: 

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.

Daniel Vandersluis
Damn, I just missed it! @ 5.2.14
John Isaacks
@John time to upgrade? ;)
Daniel Vandersluis
Note that this code will throw en error (E_NOTICE to be exactl) when given cookie doesn't exist.
Crozin
can easily be fixed with isset()
Galen
@Crozin true, but so will all of the OP's examples. Notices aren't errors, by the way.
Daniel Vandersluis
$def = (isset($_COOKE['dimaond'])) ? $_COOKIE['dimaon'] : 0.25;
Chris
They aren't errors only because PHP is fool-proof - they should be. ;)
Crozin
+2  A: 

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

Cfreak
It would only be subjective if I had asked which method was better. But there is nothing subjective about the length of characters one way is compared to another.
John Isaacks
Yes there is. The fact that the length shouldn't matter, code "readability" more important than saving 50 characters.
Galen
@Galen, whether or not I should do it may be a subjective question. But that is not the question I asked. If I had asked that, than people could argue all day long. But I asked which way was the shortest. This has a answer that can be measured. There is no debate on whether one answer has more characters than another answer.
John Isaacks
youre missing the point
Galen
@Galan, I don't want to sound rude, but I think you are. In my opinion 20 short single line assignments in a sequence is easier to read than 20 `if` statements. That is the very reason why I wanted to do this, was to make the code easier for myself to follow. However, me thinking that the code is easier to read this way is subjective, and you telling me that it is easier to read another way is also subjective. I did not start this thread for a subjective conversation about which is better. I started it to get a concrete answer on how to write it the way I feel is easier to understand.
John Isaacks
+4  A: 

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");
M42
+2  A: 

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;
Galen
+1 for recommendation of using a config file to define default values.
John Isaacks