What is '?' for?: SOLVED

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
timsewell
Forum Newbie
Posts: 21
Joined: Tue Feb 26, 2008 5:43 am
Location: Hove, England

What is '?' for?: SOLVED

Post 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!
Last edited by timsewell on Tue May 13, 2008 8:22 am, edited 1 time in total.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: What is '?' for?

Post 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 :D
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: What is '?' for?

Post 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
timsewell
Forum Newbie
Posts: 21
Joined: Tue Feb 26, 2008 5:43 am
Location: Hove, England

Re: What is '?' for?

Post by timsewell »

Thanks very much. That makes sense to me now.
Post Reply