14.07.2013 Views

Essentials of Javascript - Cultural View

Essentials of Javascript - Cultural View

Essentials of Javascript - Cultural View

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

?: 55<br />

echo $vehicle;<br />

?><br />

'feet' );<br />

Due to an unfortunate error in the language grammar, the implementation <strong>of</strong> ?: in PHP uses the incorrect<br />

associativity when compared to other languages, and given a value <strong>of</strong> T for arg, the PHP equivalent <strong>of</strong> the above<br />

example would yield the value horse instead <strong>of</strong> train as one would expect. To avoid this, nested parenthesis are<br />

needed, as in this example:<br />

<br />

(($arg == 'A') ? 'airplane' :<br />

(($arg == 'T') ? 'train' :<br />

(($arg == 'C') ? 'car' :<br />

(($arg == 'H') ? 'horse' :<br />

'feet'))));<br />

This will produce the correct result <strong>of</strong> train being printed to the screen.<br />

CFML (Railo only)<br />

<br />

arg = "T";<br />

vehicle = ( ( arg == 'B' ) ? 'bus' :<br />

<br />

( arg == 'A' ) ? 'airplane' :<br />

( arg == 'T' ) ? 'train' :<br />

( arg == 'C' ) ? 'car' :<br />

( arg == 'H' ) ? 'horse' :<br />

#vehicle#<br />

Result type<br />

'feet' );<br />

Clearly the type <strong>of</strong> the result <strong>of</strong> the ?: operator must be in some sense the type unification <strong>of</strong> the types <strong>of</strong> its second<br />

and third operands. In C this is accomplished for numeric types by arithmetic promotion; since C does not have a<br />

type hierarchy for pointer types, pointer operands may only be used if they are <strong>of</strong> the same type (ignoring type<br />

qualifiers) or one is void or NULL. It is undefined behaviour to mix pointer and integral or incompatible pointer<br />

types; thus<br />

number = spell_out_numbers ? "forty-two" : 42;<br />

will result in a compile-time error in most compilers.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!