NYCPHP Meetup

NYPHP.org

[nycphp-talk] Weird Switch Behavior

Glenn glenn310b at mac.com
Sat Sep 13 09:32:49 EDT 2008


Interesting and subtle code.
IMHO, the best solution is to make sure every hash
item has a key/value pair when the hash is created.
Someone may have to work with the code in the future
and if the hash creation is done in some required/included
file like a class, it might be more time consuming to determine
what was happening.

Glenn

On Sep 9, 2008, at 10:31 PM, Michael B Allen wrote:

> Can someone explain why the below switch matches the 0 element?
>
> $ cat switch.php
> <?php
>
> $tmp = array(
>         'foo' => 1,
>         'bar' => 2,
>         'zap',
> );
>
> foreach ($tmp as $key => $val) {
>         switch ($key) {
>                 case 'foo':
>                 case 'bar':
>                         echo "[$key][$val]\n";
>                         break;
>         }
> }
>
> $ php -f switch.php
> [foo][1]
> [bar][2]
> [0][zap]
>
> -- 
> Michael B Allen
> PHP Active Directory SPNEGO SSO
> http://www.ioplex.com/
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
>




More information about the talk mailing list