Re: PHP Brain Teasers

From: Date: Tue, 26 Jun 2007 23:51:15 +0000
Subject: Re: PHP Brain Teasers
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-257825@lists.php.net to get a copy of this message
Robert Cummings wrote: > On Wed, 2007-06-27 at 00:24 +0100, Colin Guthrie wrote: >> Daniel Brown wrote: >>> What the hell? Why not start a thread that can be fun and >>> challenging for all of us. It's something I haven't seen done >> Here's another quickie: Dead simple one! >> >> <?php >> >> function x($x) >> { >> switch ($x) >> { >> case 1: return 'bitten'; >> case 2: return 'shy'; >> } >> } > > Hmmm... would be better as (: > > <?php > > function x( $x ) > { > echo 'bitten'; > echo 'shy'; > echo 'shy'; > } > > ?> I guess you could call that different syntax, same semantics! > Here's another one: > > <?php > > class money > { > var $pennies = 0; > > function money( $amount=0 ) > { > $this->pennies = $amount; > } > > function save() > { > return true; > } > } > > class account > { > var $total = 0; > > function account( $init=0 ) > { > $this->total = $init; > } > > function add( $money ) > { > $this->total += $money->pennies; > } > } > > $earned = new account(); > $penny = new money( 1 ); > > if( $penny->save() ) > { > $earned->add( $penny ); > } > > ?> Nice :p Col

« previous php.general (#257825) next »