NYCPHP Meetup

NYPHP.org

Why doesn't this work: chained ?: expressions

George Webb gw.nyphp at gwprogramming.com
Thu Jan 16 20:49:24 EST 2003


Hi PHP Team!

	I'm sure "associativity" is the answer to why doesn't this work
as desired, but perhaps someone could suggest how to get this sexy expression
chain to work, and still look sexy:


-----
$input = $argv[1];

$output = preg_match ( '/one/i', $input ) ? '01'
        : preg_match ( '/two/i', $input ) ? '02'
        : preg_match ( '/three/i', $input ) ? '03'
        : preg_match ( '/four/i', $input ) ? '04'
        : '00';

echo "$input => $output\
";
-----


Here is some sample output:
	
one => 04
two => 04
three => 04
four => 04
five => 00
asdf => 00

	Obviously this is not the desired result.  Must we fix this
with parentheses, and if so how?  Will it become ugly?  Should I just
use a bunch of "if" statements?

	Thanks for your thoughts!


Best, George.

George Webb
gw.nyphp at gwprogramming.com
(802) 283-4352



More information about the talk mailing list