Page 1 of 1

ternary operator php >= 5.3

Posted: Thu Aug 19, 2010 7:33 am
by shawngoldw
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

Re: ternary operator php >= 5.3

Posted: Thu Aug 19, 2010 10:23 am
by MindOverBody
Yap, possible.

Re: ternary operator php >= 5.3

Posted: Fri Aug 20, 2010 11:01 am
by shawngoldw
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:

Code: Select all

$var = isset($_GET["var"]) ? $_GET["var"] : "default";
How would this translate to the simpler version of the ternary operator? If you were to use this:

Code: Select all

$var = isset($_GET["var"]) ? : "default";
You would get true if it is set and default if it isn't, right?

Can you do this?:

Code: Select all

$var = $_GET["var"] ? : "default";
Does that work?


Thanks,
Shawn

Re: ternary operator php >= 5.3

Posted: Fri Aug 20, 2010 1:33 pm
by Weirdan

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"

Re: ternary operator php >= 5.3

Posted: Fri Aug 20, 2010 1:57 pm
by PHPHorizons
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.

Code: Select all

$foo = filter_input(INPUT_GET, 'foo', FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 10))) ?: 1;
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

Re: ternary operator php >= 5.3

Posted: Fri Aug 20, 2010 2:58 pm
by shawngoldw
@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:

Code: Select all

$var = isset($_GET["var"]) ? $_GET["var"] : "default";
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