Shorthand conditional to define a variable based on the existence of another variable in PHP
Asked Answered
J

3

19

Essentially, I'd love to be able to define a variable as one thing unless that thing doesn't exist. I swear that somewhere I saw a shorthand conditional that looked something like this:

$var=$_GET["var"] || "default";

But I can't find any documentation to do this right, and honestly it might have been JS or ASP or something where I saw it.

I understand that all that should be happening in the above code is just to check if either statement returns true. But I thought I saw someone do something that essentially defined a default if the first failed. Is this something anyone knows about and can help me? Am I crazy? It just seems redundant to say:

$var=($_GET["var"]) ? $_GET["var"] : "default";

or especially redundant to say:

if ($_GET["var"]) { $var=$_GET["var"]; } else { $var="default"; }

Thoughts?

Juniorjuniority answered 17/9, 2010 at 19:40 Comment(5)
Yes it's stupid php can't do this, anyone know if it can do this as of 5.6?Cinquain
@DominicTobias The accepted answer works in 5.3+Juniorjuniority
Possible duplicate of Setting default values (conditional assignment)Florist
@MatthijsWessels that question is asking how best to default a possibly-set variable. I was asking for a specific operator to get one variable (not necessarily the same variable) and fall back to another. The accepted answers for each question reflect this difference. They are similar but distinct questions, with different accepted answers, and I don't feel there's any modification to this question needed.Juniorjuniority
Hmm, after reading what conditional assignment in Ruby is exactly (what the other question was closed as duplicate was about), I guess there is a subtle difference.Florist
H
16

Matthew has already mentioned the only way to do it in PHP 5.3. Note that you can also chain them:

$a = false ?: false ?: 'A'; // 'A'

This is not the same as:

$a = false || false || 'A'; // true

The reason why is that PHP is like most traditional languages in this aspect. The logical OR always returns true or false. However, in JavaScript, the final expression is used. (In a series of ORs, it will be the first non-false one.)

var a = false || 'A' || false; // 'A' 
var b = true && 'A' && 'B';    // 'B';
Hufford answered 17/9, 2010 at 19:58 Comment(1)
Other Matthew removed his answer because he didn't like that the answer didn't follow his personal best practices, so he removed it. Therefore I'm marking this as correct because it expands on the original answer. For the record, other Matthew's concerns are expressed in Fanis's answer. They are valid concerns, but the answer to my question is in fact ?:, as shown here.Juniorjuniority
B
8

In such cases you should be checking for existence of the variable in $_GET and then whether it's valid for your parameters. For example:

$var = (isset($_GET["var"]) && $_GET['var'] !== '') ? $_GET["var"] : "default";

However, this can become pretty unreadable pretty quickly. I'd say keep it readable by first initializing your variable to a safe default, and then overwriting that with an external one, if that's valid:

$var = "default";
if (isset($_GET['var') && $_GET['var'] !== '') {
    $var = $_GET['var] ;
}

As for your first example, $var=$_GET["var"] || "default"; exists in Javascript: var someVar = incomingVar || "default";

Brackett answered 17/9, 2010 at 19:57 Comment(2)
@Ben, this should probably be the accepted answer. It's a little longer, but it avoids warnings. Also, the check against !== '' is not really about existence, though it may be useful for certain cases.Searchlight
@MatthewFlaschen This doesn't actually answer the question I had though. It raises valid concerns, so I've upvoted it. But the question was if there was a way to accomplish what you did, shorthand.Juniorjuniority
W
3

I've always used empty.

$var = !empty($_GET['var'])?$_GET['var']:'default';
Wassyngton answered 2/6, 2014 at 15:20 Comment(1)
I suppose if you're using $_GET, you'll never get a boolean response so empty is indistinguishable from isset, but this seems like a worse way to do it in other situations: $var = !empty($other_var)?$other_var:true will make $var always equal true. Although, I guess, so will the JS notation var = other_var || true.Juniorjuniority

© 2022 - 2024 — McMap. All rights reserved.