Viewed   63 times

Possible Duplicate:
Reference - What does this symbol mean in PHP?

I've been doing conditionals with if/else or a year or so now. Looking at some new code, I'm seeing a conditional that appears to use ? and : instead of if and else. I'd like to learn more about this, but I am not sure what to google to find articles explaining how it works. How can I do it?



It's the Ternary Operator.

Basic usage is something like

$foo = (if this expressions returns true) ? (assign this value to $foo) : (otherwise, assign this value to $foo)

It can be used for more than assignment though, it looks like other examples are cropping up below.

I think the reason you see this in a lot of modern, OO style PHP is that without static typing you end up needing to be paranoid about the types in any particular variable, and a one line ternary is less cluttered than a 7 line if/else conditional.

Also, in deference to the comments and truth in naming, read all about the ternary operators in computer science.

Wednesday, November 23, 2022

Ternary operators are tricky thing in PHP, as they are left-associative (unlike all other languages, where it's right-associative). You will need to use parenthesis to tell PHP what you want exactly in this case:

echo ($projectURL ? $projectURL : ($project['project_url'] ? $project['project_url'] : $project['project_id']));
Sunday, October 30, 2022

Check out the Wikipedia page on the Netflix Prize and its discussion forum. Also, the somewhat related 2009 GitHub Contest is a good source for full source code on a number of different recommendation engines. And obviously there's also the Wikipedia page on the topic itself, which has some decent links.

If you start writing your own, you'll want to use a corpus. I'd actually recommend using the Netflix Prize's data set. Just carve the data set into two pieces. Train on the first piece and score your algorithm on the second piece.

Addenda: A somewhat related and scary application of this sort of thing is predicting demographic information: a user's gender, age, household income, IQ, sexual orientation, etc. You could probably do most of these attributes with the Netflix Prize dataset with a fairly high degree of accuracy. Fortunately everyone in that dataset is just a number.

Thursday, October 27, 2022

The PHP Manual has a (sadly mostly empty) chapter on PHP internals.

The main development mailing list is You can sign up via and/or use Markmail to search the archives.

The git repository for PHP is located on, but there is also a mirror on GitHub.

For browsing the source code you should use the cross reference tool.

The PHP wiki has a list of various resources on PHP development (blog posts, books, slides, etc).

In particular there is an (older) book by Sara Golemon: Extending and Embedding PHP.

A more current and ongoing effort is

If you have questions, you should try the #php.pecl room on efnet.

Also see this presentation by Sebastian Bergmann about Compiler Internals:


And make sure to check Nikic's blog. He's got a number of posts on how to read the source:


In addition to that, you can check the PHP Credits for individual contributers:


A number of them run their own blogs which might contain more information.

Wednesday, August 17, 2022

By looking at your code snippet, it simply makes no sense to use a ternary operation here.

All you can do is:

if (!packageDict.ContainsKey(desc))
    packageDict.Add(desc, subtotal)
Saturday, October 8, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :