ternary-operator

C#'s null coalescing operator (??) in PHP

て烟熏妆下的殇ゞ 提交于 2019-11-27 01:23:28
Is there a ternary operator or the like in PHP that acts like ?? of C#? ?? in C# is clean and shorter, but in PHP you have to do something like: // This is absolutely okay except that $_REQUEST['test'] is kind of redundant. echo isset($_REQUEST['test'])? $_REQUEST['test'] : 'hi'; // This is perfect! Shorter and cleaner, but only in this situation. echo null? : 'replacement if empty'; // This line gives error when $_REQUEST['test'] is NOT set. echo $_REQUEST['test']?: 'hi'; PHP 7 adds the null coalesce operator : // Fetches the value of $_GET['user'] and returns 'nobody' // if it does not exist

Which coding style you use for ternary operator? [closed]

不问归期 提交于 2019-11-27 00:35:20
I keep it in single line, if it's short. Lately I've been using this style for longer or nested ternary operator expressions. A contrived example: $value = ( $a == $b ) ? 'true value # 1' : ( $a == $c ) ? 'true value # 2' : 'false value'; Personally which style you use, or find most readable? Edit: (on when to use ternary-operator) I usually avoid using more than 2 levels deep ternary operator. I tend prefer 2 levels deep ternary operator over 2 level if-else, when I'm echoing variables in PHP template scripts. The ternary operator is generally to be avoided, but this form can be quite

One-line list comprehension: if-else variants

♀尐吖头ヾ 提交于 2019-11-26 23:30:29
It's more about python list comprehension syntax. I've got a list comprehension that produces list of odd numbers of a given range: [x for x in range(1, 10) if x % 2] This makes a filter - I've got a source list, where I remove even numbers ( if x % 2 ). I'd like to use something like if-then-else here. Following code fails: >>> [x for x in range(1, 10) if x % 2 else x * 100] File "<stdin>", line 1 [x for x in range(1, 10) if x % 2 else x * 100] ^ SyntaxError: invalid syntax There is a python expression like if-else: 1 if 0 is 0 else 3 How to use it inside a list comprehension? shx2 x if y

Angularjs if-then-else construction in expression

北城以北 提交于 2019-11-26 22:35:00
问题 Can I somehow use if-then-else construction (ternary-operator) in angularjs expression, for example I have function $scope.isExists(item) that has to return bool value. I want something like this, <div ng-repeater="item in items"> <div>{{item.description}}</div> <div>{{isExists(item) ? 'available' : 'oh no, you don't have it'}}</div> </div> I know that I can use function that returns string, I'm interesting in possibility of using if-then-else construction into expression. Thanks. 回答1:

if/else vs ternary operator

两盒软妹~` 提交于 2019-11-26 22:04:17
问题 Considering the evaluation time, are following two equivalent? if(condition1) { //code1 } else { //code2 } condition1 ? code1 : code2 Or they are just syntactically different? 回答1: The difference is that the latter station can be used to return a value based on a condition. For example, if you have a following statement: if (SomeCondition()) { text = "Yes"; } else { text = "No"; } Using a ternary operator, you will write: text = SomeCondition() ? "Yes" : "No"; Note how the first example

Stacking Multiple Ternary Operators in PHP

天大地大妈咪最大 提交于 2019-11-26 21:56:45
This is what I wrote : $Myprovince = ( ($province == 6) ? "city-1" : ($province == 7) ? "city-2" : ($province == 8) ? "city-3" : ($province == 30) ? "city-4" : "out of borders" ); But for every field I got the value city-4 . I want to use ternary operators instead of switch/if because I want to experiment and see how it would be done. What's the problem with this code? codaddict Others have already suggested the right way of doing it but if you really want to use ternary operator you need to use parenthesis as: $province = 7; $Myprovince = ( ($province == 6) ? "city-1" : (($province == 7) ?

PHP syntax question: What does the question mark and colon mean? [duplicate]

隐身守侯 提交于 2019-11-26 21:41:52
Possible Duplicate: quick php syntax question return $add_review ? FALSE : $arg; What do question mark and colon mean? Thanks Paul Dixon This is the PHP ternary operator (also known as a conditional operator) - if first operand evaluates true, evaluate as second operand, else evaluate as third operand. Think of it as an "if" statement you can use in expressions. Can be very useful in making concise assignments that depend on some condition, e.g. $param = isset($_GET['param']) ? $_GET['param'] : 'default'; There's also a shorthand version of this (in PHP 5.3 onwards). You can leave out the

PHP Ternary operator clarification

纵饮孤独 提交于 2019-11-26 21:35:51
问题 I use the ternary operator quite often but I've not been able to find anything in the documentation about this and I've always wondered it. The following is a possible example: echo ($something->message ? $something->message : 'no message'); as you can see, if $something->message is correct, we return $something->message, but why write it twice? Is there a way to do something like: echo ($something->message ? this : 'no message'); Now I'm not well versed in programming theory, so it's

?: ?? Operators Instead Of IF|ELSE

风流意气都作罢 提交于 2019-11-26 21:26:43
public string Source { get { /* if ( Source == null ){ return string . Empty; } else { return Source; } */ return Source ?? string.Empty; } set { /* if ( Source == null ) { Source = string . Empty; } else { if ( Source == value ) { Source = Source; } else { Source = value; } } */ Source == value ? Source : value ?? string.Empty; RaisePropertyChanged ( "Source" ); } } Can I use ?: ?? operators EXACTLY as If/Else? My Question : How to write the following with ?: ?? operators [ 1 ] if ( Source == null ){ // Return Nothing } else { return Source; } [ 2 ] if ( Source == value ){ // Do Nothing }

inline conditionals in angular.js

我只是一个虾纸丫 提交于 2019-11-26 21:16:06
I was wondering if there is a way in angular to conditionally display content other than using ng-show etc. For example in backbone.js I could do something with inline content in a template like: <% if (myVar === "two") { %> show this<% } %> but in angular, I seem to be limited to showing and hiding things wrapped in html tags <p ng-hide="true">I'm hidden</p> <p ng-show="true">I'm shown</p> What is the recommended way in angular to conditionally show and hide inline content in angular just using {{}} rather than wrapping the content in html tags? Ben Lesh EDIT: 2Toad's answer below is what you