Hi Mike,
Maybe a simple way around this is just to use:
switch("$key")
It makes it pretty clear that you are only dealing with strings, and
you won't have to deal with casting issues.
Regards,
John Campbell
___
New York PHP Community Talk Mailing List
Hi Mike,
It kind of sounds like you are talking about two different things
or at least the confusion involves two different topics. Basically, if
you are going to use a Switch statement it's a good practice to keep
the case expression within quotation, if you want to get desired
results.
Also,
At 10:44 PM 9/9/2008, Michael B Allen wrote:
On Tue, Sep 9, 2008 at 10:31 PM, Michael B Allen <[EMAIL PROTECTED]> wrote:
> Can someone explain why the below switch matches the 0 element?
>
> $ cat switch.php
>
> $tmp = array(
>'foo' => 1,
>'bar' => 2,
>'zap',
> );
>
> for
On Tue, Sep 9, 2008 at 10:56 PM, Hans Zaunere <[EMAIL PROTECTED]> wrote:
>> Nevermind - $key is an int so 'foo' is being cast to an int which
>> evaluates to 0.
>>
>> I think I would prefer that switch be a little more explicit.
>>
>> >switch ($key) {
>> >case 'foo':
>> >
Michael B Allen wrote:
On Tue, Sep 9, 2008 at 10:31 PM, Michael B Allen <[EMAIL PROTECTED]> wrote:
Can someone explain why the below switch matches the 0 element?
$ cat switch.php
1,
'bar' => 2,
'zap',
);
foreach ($tmp as $key => $val) {
Nevermind - $key is an int so 'foo' is
> > Can someone explain why the below switch matches the 0 element?
> >
> > $ cat switch.php
> > >
> > $tmp = array(
> >'foo' => 1,
> >'bar' => 2,
> >'zap',
> > );
> >
> > foreach ($tmp as $key => $val) {
>
> Nevermind - $key is an int so 'foo' is being cast to an int whic
Hi Michael,
Switch isn't the problem. Arrays are going to behave like Arrays no
matter how you choose to loop through them.
$tmp = array('foo','bar','zap'); is the same as typing
$tmp = array(0=>'foo', 1='bar', 2='zap');
If you don't specify the index, PHP will start using numbers starting
a
On Tue, Sep 9, 2008 at 10:31 PM, Michael B Allen <[EMAIL PROTECTED]> wrote:
> Can someone explain why the below switch matches the 0 element?
>
> $ cat switch.php
>
> $tmp = array(
>'foo' => 1,
>'bar' => 2,
>'zap',
> );
>
> foreach ($tmp as $key => $val) {
Nevermind - $key
Can someone explain why the below switch matches the 0 element?
$ cat switch.php
1,
'bar' => 2,
'zap',
);
foreach ($tmp as $key => $val) {
switch ($key) {
case 'foo':
case 'bar':
echo "[$key][$val]\n";