Assign variable to 1 of 3 other variables.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Assign variable to 1 of 3 other variables.

Post by pickle »

Hi all,

I want to assign a variable to the "best" of three variables. By that I mean, for example, I want to assign $x to:
  • $a if $a is set
  • $b if $a is not set
  • $c if neither $a or $b are set.
In Javascript one could do this:

Code: Select all

$x = $a || $b || $c;
but PHP assigns a boolean value to $x.

I'm currently doing this with a couple ternary operators, but I'm wondering if there's a cleaner way.

Thanks.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Assign variable to 1 of 3 other variables.

Post by requinix »

Using || you would exploit short-circuiting like

Code: Select all

($x = $a) || ($x = $b) || ($x = $c); // needs the parentheses
Personally I'd prefer the ternary operator.

Code: Select all

$x = $a ?: $b ?: $c; // remember that it associates like ($a ?: $b) ?: $c
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Assign variable to 1 of 3 other variables.

Post by pickle »

As far as I know, ?: isn't a valid PHP operator. I've changed it a bit to:

[syntax]$x = $a ? $a : ($b ? $b : $c)[/syntax]Which is succinct, but not as pretty as I'd hoped.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Assign variable to 1 of 3 other variables.

Post by requinix »

$x ?: $y is available in 5.3+ and is equivalent to $x ? $x : $y. Shorthand.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Assign variable to 1 of 3 other variables.

Post by pickle »

Ah, I'm running 5.2
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Assign variable to 1 of 3 other variables.

Post by requinix »

Yeah, without the shorthand you have to use parentheses :(
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Assign variable to 1 of 3 other variables.

Post by AbraCadaver »

You'll get notices though. Your OP said "if $a is set, etc.". This code is not checking for isset but for non false value.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Assign variable to 1 of 3 other variables.

Post by pickle »

I used the term "is set" because it was more succinct than "has the boolean value of false". In practice, $a, $b can be set to FALSE.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Assign variable to 1 of 3 other variables.

Post by Weirdan »

User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Assign variable to 1 of 3 other variables.

Post by requinix »

I wish they would alter the short ?: (not necessarily the full ternary form) so that it suppressed undefined variable/offset warnings, like how isset() does with things like

Code: Select all

isset($no_such_variable["key 1"]["key 2"]))
which is equivalent to but does not work exactly the same way as

Code: Select all

isset($no_such_variable, $no_such_variable["key 1"], $no_such_variable["key 1"]["key 2"])
Then dealing with default URL/form variables is much cleaner.

Code: Select all

$page = ($_GET["page"] ?: 1);
// which currently warns so you have to
$page = (isset($_GET["page"]) ? $_GET["page"] : 1);
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Assign variable to 1 of 3 other variables.

Post by Weirdan »

Post Reply