|
Posted by Martie on 05/10/06 20:15
On 10 May 2006 07:15:57 -0700, kenoli wrote...
>
>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 ":".
>
>I have had trouble generally getting good documentation on printf() and
>sprintf().
>
>Thanks,
>
>--Kenoli
>
>Kenoli Oleari
>Neighborhood Assemblies Network
>kenoli@igc.org
>510-601-8217
>http://www.horizonsofchange.com
>http://www.sfnan.org
>
The "(check condition) ? 'true' : 'false'" lets you hand define an action or an
alternate action if a condition is true or not. In the example you had above, if
"$arr1 === $arr2" id true, then the left side of the ":" would be used. If there
was no match for that condition, then the right side of the ":" would be used
instead.
From what I remember using sprintf(), it allows you to format numeric values
consistently, though there's probably a bigger purpose. As an example if you
wanted all your variables to have a dollar sign and 2 digits after the decimal
you could use...
sprintf("\$%0.2f", $var);
--
NewsGuy.Com 30Gb $9.95 Carry Forward and On Demand Bandwidth
Navigation:
[Reply to this message]
|