Page 1 of 1
What is '?' for?: SOLVED
Posted: Tue May 13, 2008 5:53 am
by timsewell
Sorry to ask what must seem like a ridiculous question but I see a '?' used in a lot of code here and elsewhere but I can't seem to find a reference to its use in any of my books and obviously Googling '?' doesn't help either.
Can someone put me out of my misery and tell me what it's function or usage is?
Edit: obviously I don't mean the '?' in '<?php' - I'm not that new!
Re: What is '?' for?
Posted: Tue May 13, 2008 6:04 am
by aceconcepts
I don't have an official technical definition however, I have always used ? to determine when variables and comparisons are being utilised.
However, "?" is also used in other programming languages. I remember using it with ColdFusion.
Hope this helps - I'm sure someone else will be able to offer a more definitive response

Re: What is '?' for?
Posted: Tue May 13, 2008 6:08 am
by Apollo
You probably mean the
ternary operator (search for 'ternary' on the page)
It's an expression like
(condition) ? expression1 : expression2, in which
expression1 gets evaluated if
condition is true, otherwise
expression2.
For example:
Code: Select all
$a = 5;
$b = ($a > 3) ? 10 : 7;
// $b is now 10
// if $a would have not been >3 then $b would have been 7
Re: What is '?' for?
Posted: Tue May 13, 2008 8:04 am
by timsewell
Thanks very much. That makes sense to me now.