I just stumbled onto a neat trick in php 5.3. The ternary operator has been modified a little, you can now leave out the middle expression:
From the php manual, "Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise."
I always found myself doing something like:
var = expr1 ? expr1 : expr3
now you can just do:
var = expr1 ?: expr3
Now we don't have to repeat ourselves.
Just thought some of you would be interested to hear this.
Also, I don't have php 5.3 on my server, I'll be updating to it in about 2 weeks. So I haven't actually been able to test this.
Shawn
ternary operator php >= 5.3
Moderator: General Moderators
-
shawngoldw
- Forum Contributor
- Posts: 212
- Joined: Mon Apr 05, 2010 3:38 pm
- MindOverBody
- Forum Commoner
- Posts: 96
- Joined: Fri Aug 06, 2010 9:01 pm
- Location: Osijek, Croatia
Re: ternary operator php >= 5.3
Yap, possible.
-
shawngoldw
- Forum Contributor
- Posts: 212
- Joined: Mon Apr 05, 2010 3:38 pm
Re: ternary operator php >= 5.3
Someone who has php 5.3, how would this work? I've been thinking about it and it doesn't seem like it would work the way I first hoped.
Say you did this:
How would this translate to the simpler version of the ternary operator? If you were to use this:
You would get true if it is set and default if it isn't, right?
Can you do this?:
Does that work?
Thanks,
Shawn
Say you did this:
Code: Select all
$var = isset($_GET["var"]) ? $_GET["var"] : "default";
Code: Select all
$var = isset($_GET["var"]) ? : "default";
Can you do this?:
Code: Select all
$var = $_GET["var"] ? : "default";
Thanks,
Shawn
Re: ternary operator php >= 5.3
Code: Select all
$ php -ddisplay_errors=1 -derror_reporting=`php -r 'echo E_ALL|E_STRICT|E_DEPRECATED;'` -r '$var = $_GET["var"] ? : "default"; var_dump(phpversion());'
Notice: Undefined index: var in Command line code on line 1
string(5) "5.3.3"
- PHPHorizons
- Forum Contributor
- Posts: 175
- Joined: Mon Sep 14, 2009 11:38 pm
Re: ternary operator php >= 5.3
Hello shawngoldw,
You have the right idea about a simplication of initializing get/post values, but I think the intended way to use the short ternary is with filter_input. filter_input has built in isset() functionality, but returns the value, if it is set and passes the validation. Therefore, the short ternary is a great way to provide a default to filter_input.
filter_input was intimidating to me at first, because it's hard to remember all the settings, but it really makes things a lot more concise and it also performs type conversion. I.e., $foo in this case is guaranteed to be a integer.
Cheers
You have the right idea about a simplication of initializing get/post values, but I think the intended way to use the short ternary is with filter_input. filter_input has built in isset() functionality, but returns the value, if it is set and passes the validation. Therefore, the short ternary is a great way to provide a default to filter_input.
Code: Select all
$foo = filter_input(INPUT_GET, 'foo', FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 10))) ?: 1;Cheers
-
shawngoldw
- Forum Contributor
- Posts: 212
- Joined: Mon Apr 05, 2010 3:38 pm
Re: ternary operator php >= 5.3
@Weirdan: So that means my third block does not work. I did not think it would. Thanks for varifying that
@PHPHorizons: filter_input is way more than I'm talking about. I can just do this:
This will merely check if var has a value. That is all i am wondering if it can do. I do see what you're saying though.
Thanks
Shawn
@PHPHorizons: filter_input is way more than I'm talking about. I can just do this:
Code: Select all
$var = isset($_GET["var"]) ? $_GET["var"] : "default";
Thanks
Shawn