Asked  2 Years ago    Answers:  5   Viewed   58 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?

 Answers

5

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
1

This is the conditional operator.

$x ? $y : $z

means "if $x is true, then use $y; otherwise use $z".

It also has a short form.

$x ?: $z

means "if $x is true, then use $x; otherwise use $z".

People will tell you that ?: is "the ternary operator". This is wrong. ?: is a ternary operator, which means that it has three operands. People wind up thinking its name is "the ternary operator" because it's often the only ternary operator a given language has.

Sunday, November 13, 2022
 
1

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

The main development mailing list is [email protected] You can sign up via php.net and/or use Markmail to search the archives.

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

For browsing the source code you should use the lxr.php.net 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 http://www.phpinternalsbook.com

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


Also see this presentation by Sebastian Bergmann about Compiler Internals:

  • http://www.scribd.com/doc/18171982/PHP-Compiler-Internals

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

  • http://nikic.github.com/

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

  • http://www.php.net/credits.php

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

Wednesday, August 17, 2022
 
5

I'll mention a few that i think are excellent resources but that i haven't seen mentioned on SO. They are all free and freely available on the Web (links supplied).

Data Analysis Examples A collection of individual examples from the UCLA Statistics Dept. which you can browse by major category (e.g., "Count Models", "Multivariate Analysis", "Power Analysis") then download examples with complete R code under any of these rubrics (e.g., under "Count Models" are "Poisson Regression", "Negative Binomial Regression", and so on).

Verzani: SimpleR: Using R for Introductory Statistics A little over 100 pages, and just outstanding. It's easy to follow but very dense. It is a few years old, still i've only found one deprecated function in this text. This is a resource for a brand new R user; it also happens to be an excellent statistics refresher. This text probably contains 20+ examples (with R code and explanation) directed to fundamental statistics (e.g., hypothesis testing, linear regression, simple simulation, and descriptive statistics).

Statistics with R (Vincent Zoonekynd) You can read it online or print it as a pdf. Printed it's well over 1000 pages. The author obviously got a lot of the information by reading the source code for the various functions he discusses--a lot of the information here, i haven't found in any other source. This resource contains large sections on Graphics, Basic Statistics, Regression, Time Series--all w/ small examples (R code + explanation). The final three sections contain the most exemplary code--very thorough application sections on Finance (which seems to be the author's professional field), Genetics, and Image Analysis.

Friday, August 5, 2022
 
5

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 :
 

Browse Other Code Languages