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
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Wed Jun 05, 2013 12:57 pm
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:
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.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Wed Jun 05, 2013 1:11 pm
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
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Wed Jun 05, 2013 3:13 pm
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.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Wed Jun 05, 2013 3:16 pm
$x ?: $y is available in 5.3+ and is equivalent to $x ? $x : $y. Shorthand.
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Wed Jun 05, 2013 3:22 pm
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.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Wed Jun 05, 2013 6:22 pm
Yeah, without the shorthand you have to use parentheses
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Thu Jun 06, 2013 1:26 pm
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.
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Thu Jun 06, 2013 1:57 pm
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.
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Fri Jun 07, 2013 1:16 pm
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Fri Jun 07, 2013 2:47 pm
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);
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Fri Jun 07, 2013 6:15 pm