473,387 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Ternary operator OR if condition ?

296 100+
Which is better -->


Expand|Select|Wrap|Line Numbers
  1. if($mData == "")
  2.     $mData = 0;
  3. else
  4.     $mData = 1;
OR

Expand|Select|Wrap|Line Numbers
  1. $mData = $mData == "" ? 0 : 1;
Jun 5 '08 #1
3 1329
Markus
6,050 Expert 4TB
Depends on the way you are using it.

In the way shown above, it'd be much easier - and less writing - to use the latter way.
Jun 5 '08 #2
coolsti
310 100+
You could also go further and get a bit messy, which I have seen in some code and which I (very rarely) use in my own:

Expand|Select|Wrap|Line Numbers
  1. $resvar = ($mData == "") ? 0 : (($mData == 1) ?  1 : 2);
  2.  
Am not sure how many chains of this you can use, and if there is any advantage of going beyond the first usage of the ternary OR.
Jun 5 '08 #3
Atli
5,058 Expert 4TB
In the samples you posted above, the Ternary operator would be a better fit.

It's main purpose would be to initialize variables based on simple boolean expressions, such as it's current value.

The if construct is a control structure, meant to control the flow of your code. It should be used when you need to execute different statements based on the results of a boolean expression.
Jun 5 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: praba kar | last post by:
Dear All, I am new to Python. I want to know how to work with ternary operator in Python. I cannot find any ternary operator in Python. So Kindly clear my doubt regarding this ...
24
by: gupta.keshav | last post by:
HI, Is there any situation which can be handled by ternary operator but not with if else blocks? Thanks Keshav
6
by: glongword | last post by:
As the assert macro should evaluate to a void expression, it should not have an 'if statement' in its definition. Does this necessitate the existence of a ternary conditional operator? Is there a...
6
by: Robert Zurer | last post by:
In his paper on Coding Standard found on http://www.idesign.net/idesign/DesktopDefault.aspx Juval Lowy discourages the use of the ternary conditional operator with no specific reason given. ...
48
by: Daniel Crespo | last post by:
Hi! I would like to know how can I do the PHP ternary operator/statement (... ? ... : ...) in Python... I want to something like: a = {'Huge': (quantity>90) ? True : False} Any...
15
by: Arthur Dent | last post by:
Hi all, im just curious if anyone knows..... With .NET 2, VB didnt happen to get a true ternary operator, did it? Stuck away in a corner somewhere? By ternary, i mean something like C's a?b:c...
20
by: Udo A. Steinberg | last post by:
Hi all, In a ternary statement such as: x = (cond ? a : b); it is obviously guaranteed that "x" will be equal to "a" only if the condition "cond" holds. Assuming that "a" is a memory location,...
5
by: PerlPhi | last post by:
hi,,, while ago i was wondering why do some programmers rarely uses the ternary operator. wherein it is less typing indeed. i believe in the classic virtue of Perl which is laziness. well let me show...
7
by: kretik | last post by:
I'm sure this is a popular one, but after Googling for a while I couldn't figure out how to pull this off. Let's say I have this initializer on a class: def __init__(self, **params): I'd...
4
by: raiderdav | last post by:
I understand how the ternary operator (question mark - ?) works in an if/else setting, but what does it mean when used as a type? For instance, I'm trying to add a get/set in some existing code,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.