Hey, how did that small if statement work in php?
something like.. (variable == something : (this is true) ?(this is false))?
Hey, how did that small if statement work in php?
something like.. (variable == something : (this is true) ?(this is false))?
What the hell are you talking 'bout man?
EDIT: oh, it works something like this:
Code:if(condition)
Jeremy | jfein.net
I'm impressed. There really is a shorter way of doing it, however you could be a little clearer in your question. It was a little bit difficult to understand what you were asking about at first.
I like this compact statement, but it can also be incredibly confusing to read, so use with discretion.
Here's how it works... quite simply, really, but odd to wrap your brain around at first:
condition ? if true do this : else this;
Or:condition [is it true?] do this [else:] this;
So, here's an example:
($a==1) ? echo 1 : echo 'not 1';
However, this can also be embedded and you can get very compact ways of using an if within something else:
echo 'We have '.$a.' item'.($a>1?'s':'').' for sale.';
You can even layer it, where the else portion is a second such statement, allowing you a result for <1, then a result for >5 if desired, making a long statement.
But remember as I said this can become very hard to read.
Last edited by djr33; 02-03-2008 at 11:14 PM.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
Bookmarks