conditional-operator

what does this syntax ( page = $page ? $page : 'default' ) in php mean?

i'm new to php. I came across this syntax in wordpress. Can anyone explain to me the last line of that code? $page = $_SERVER['REQUEST_URI']; $page = str_replace("/","",$page); $page = str_replace(".php","",$page); **$page = $page ? $page : 'default'** the last line(bolded). thanks ...

Java ?: operator in vb.net

Is there a ?: operator equivalent in .net? eg in java I can do: retParts[0] = (emailParts.length > 0) ? emailParts[0] : ""; rather than if (emailParts.length > 0) { retParts[0] = emailParts[0]; } else { retParts[0] = ""; } I'd like to be able to do similar in VB.NET. ...

Is there, or is there ever going to be, a conditional operator in Delphi?

I kept my hands off Delphi for too long, I guess; busied myself with Java and PHP a lot over the last couple of years. Now, when I got back to doing a little Delphi job, I realised I really miss the conditional operator which is supported by both Java and PHP. On how many places would you find lines like these in your Delphi programs? ...

Use of conditional operator to select which object calls a particular method?

I have two collections, and items that are added to one or the other of those collections based on whether some criteria is met. Somewhat inadvertantly, I stumbled on the fact that it is legal to write (test(foo) ? cOne : cTheOther).add(foo); instead of if (test(foo)) { cOne.add(foo); } else { cTheOther.add(foo); } While the fir...

What is ?: in PHP 5.3?

Possible Duplicate: What is the PHP ? : operator called and what does it do? From http://twitto.org/ <?PHP require __DIR__.'/c.php'; if (!is_callable($c = @$_GET['c'] ?: function() { echo 'Woah!'; })) throw new Exception('Error'); $c(); ?> Twitto uses several new features available as of PHP 5.3: The DIR constant The ...

Can one make an assignment on conditional statement in php?

Can you make an assignment on conditional statement in php as so: if(siteName_err = isValid("sitename", $_POST['sitename'], false)) { $siteName = $_POST['sitename']; } ...

Conditional operator cannot cast implicitly?

I'm a little stumped by this little C# quirk: Given variables: Boolean aBoolValue; Byte aByteValue; The following compiles: if (aBoolValue) aByteValue = 1; else aByteValue = 0; But this will not: aByteValue = aBoolValue ? 1 : 0; Error says: "Cannot implicitly convert type 'int' to 'byte'." And of course, this monstr...

What is the preferred order for operands in boolean expressions?

Is there any benefit to structuring boolean expressions like: if (0 < x) { ... } instead of if (x > 0) { ... } I have always used the second way, always putting the variable as the first operand and using whatever boolean operator makes sense, but lately I have read code that uses the first method, and after getting over the initia...

Is the conditional operator slow?

I was looking at some code with a huge switch statement and an if-else statement on each case and instantly felt the urge to optimize. As a good developer always should do I set out to get some hard timing facts and started with three variants: The original code looks like this: public static bool SwitchIfElse(Key inKey, out char key,...

In a functional language, how to conditionally select elements to be used in zip- or zipWith-style function?

I am familiar with standard zipWith functions which operate on corresponding elements of two sequences, but in a functional language (or a language with some functional features), what is the most succinct way to conditionally select the pairs of elements to be zipped, based on a third sequence? This curiosity arose while scratching o...

Why doesn't Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||=)

So for binary operators on booleans, Java has &, |, ^, && and ||. Let's summarize what they do briefly here: JLS 15.22.2 Boolean Logical Operators &, ^, and | JLS 15.23 Conditional-And Operator && JLS 15.24 Conditional-Or Operator || For &, the result value is true if both operand values are true; otherwise, the result is false....

Why does the conditional operator always return an int in C#?

Possible Duplicate: Conditional operator cannot cast implicitly? When writing a statement using the conditional operator, if the either of the expressions are numeric values they are always interpreted as int type. This makes a cast necessary to assign a short variable using this operator. bool isTrue = true; int intVal = is...

Compiler error using C# conditional operator

I can't seem to find what I need on google, and bet I'll get quick answer here. String str; bool b = true; b ? str="true" : str="false"; Console.Out.WriteLine(str); that ? : syntax looks correct to me. I'm getting compiler error though. Program.cs(13,28): error CS1002: ; expected Program.cs(13,28): error CS...

How can I force Java to accept a conditional type for one of the parameters of a method call?

This question is hard to phrase, so I'm going to have to use some code samples. Basically, I have an (overloaded) method that takes 3 parameters, the last of which I overloaded. As in, sometimes the last parameter is a String, sometimes it's a double, etc. I want to call this method using a ternary conditional expression as the last para...

Why doesn't the conditional operator correctly allow the use of "null" for assignment to nullable types?

Possible Duplicates: Nullable types and the ternary operator. Why wont this work? Conditional operator assignment with nullable<value> types? This will not compile, stating "Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DateTime' and ''" task.ActualEndDate = Text...

C# Conditional Operator Not a Statement?

I have a simple little code fragment that is frustrating me: HashSet<long> groupUIDs = new HashSet<long>(); groupUIDs.Add(uid)? unique++ : dupes++; At compile time, it generates the error: Only assignment, call, increment, decrement, and new object expressions can be used as a statement HashSet.Add is documented to return a bool...

How to check if my string is equal to null?

I want to perform some action ONLY IF my string has a meaningful value. So, I tried this. if (!myString.equals("")) { doSomething } and this if (!myString.equals(null)) { doSomething } and this if ( (!myString.equals("")) && (!myString.equals(null))) { doSomething } and this if ( (!myString.equals("")) && (myString!=null)) { do...

Java conditional operator ?: result type

I'm a bit puzzled about the conditional operator. Consider the following two lines: Float f1 = false? 1.0f: null; Float f2 = false? 1.0f: false? 1.0f: null; Why does f1 become null and the second statement throws a NullPointerException? Langspec-3.0 para 15.25 sais: Otherwise, the second and third operands are of types S1 and S2...

Calling all the 3 functions while using or operator even after returning true as a result.

I am calling three functions in my code where i want to validate some of my fields. When I tries to work with the code given below. It checks only for first value until it gets false result. I want some thing like that if fisrt function returns true then it should also call next function and so on. What can be used instead of Or Operato...

Short circuiting statement evaluation -- is this guaranteed? [C#]

Hi everyone, Quick question here about short-circuiting statements in C#. With an if statement like this: if (MyObject.MyArray.Count == 0 || MyObject.MyArray[0].SomeValue == 0) { //.... } Is it guaranteed that evaluation will stop after the "MyArray.Count" portion, provided that portion is true? Otherwise I'll get a null exception...