Re: Help with ? and :



In our last episode,
<1147270557.730422.186550@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
the lovely and talented kenoli
broadcast on alt.php:

Can someone help me interpret this snippet?

printf("<p>The two arrays are %sidentical.</p>\n", $arr1 === $arr2 ? ''
: 'not ');

It is easy to see what it does. It compares two arrays and inserts
either a space or a "not" space in the template string.

The parts I don't understand are the "?" and the ":". I can find no
documentation anywhere of the "?" and the ":".

This is a conditional operator, found in C, shell programming, perl, etc.

stuffa ? stuffb : stuffc

stuffa is evaluated (that may mean being executed)

if that returns a true value stuffb is done, but if the returned value is
false, stuffc is done.

In other words:

stuffa ? stuffb : stuffc

is a way of writing

if stuffa then stuffb else stuffc (punctuated according to your favorite
language)

I have had trouble generally getting good documentation on printf() and
sprintf().

You will often get better clues reading perl or C documentation where these
kinds of things come from.


--
Lars Eighner usenet@xxxxxxxxxxxxxxx http://www.larseighner.com/
War on Terrorism: Bad News from the Sanity Front
"Tactical nuclear capabilities should be used against the bin Laden
camps in the desert of Afghanistan." -Thomas Woodrow,_Washington Times_
.