php-general Digest 26 Sep 2010 10:03:42 -0000 Issue 6960

Topics (messages 308311 through 308315):

Re: if/elseif being treated as if/if
        308311 by: tedd
        308312 by: tedd

Array question
        308313 by: MikeB
        308314 by: chris h
        308315 by: [email protected]

Administrivia:

To subscribe to the digest, e-mail:
        [email protected]

To unsubscribe from the digest, e-mail:
        [email protected]

To post to the list, e-mail:
        [email protected]


----------------------------------------------------------------------
--- Begin Message ---
At 3:54 PM -0400 9/24/10, Bob McConnell wrote:
From: tedd

 At 2:23 PM -0400 9/24/10, Bob McConnell wrote:

 >>A switch works when a single test can dispatch all possible branches.
If
you have a series of tests where each looks for a different subset of
 >>conditions, you need an elseif.

 Not so, O'wise one.

 This will work:

 switch(1)
     {
     case $a > $b:
     /* whatever
     break;

     case $c == 1:
     /* whatever
     break;

     case $d == 'this works':
     /* whatever
     break;
     }

 Granted, it's not the normal way a switch works in some other
 languages, but it does work in PHP.  :-)

That is just so wrong, it can't actually be taken seriously. There is
simply no justification for such broken logic.

Bob McConnell

I take it seriously. In fact, I think it's a very good method of making several different comparisons in one control structure. For me, it is easy to understand, document, and maintain. Obviously, your mileage varies.

But besides the point, all I was showing was that your claim --

A switch works when a single test can dispatch all possible branches. If you have a series of tests where each looks for a different subset of conditions, you need an elseif.

-- was false. I don't need an elseif and never have. There has always been a way around using an elseif. The powers that be could boot that control and I would never miss it.

Cheers,

tedd
--
-------
http://sperling.com/

--- End Message ---
--- Begin Message ---
At 9:04 PM +0100 9/24/10, Ashley Sheridan wrote:
I don't often use this type of logic, but I have used it before and it's served me well. Essentially, a switch is a glorified if statement, and I find them a lot nicer to read and write than a series of if/elseif blocks.

Thanks,
Ash

Ash:

Exactly my take.

I wouldn't live next door to an ELSEIF even if it lived in a good neighborhood. :-)

However, I always thought (maybe in error) that the switch control (CASE statement) was derived from the computed GOTO rather than from the three-way IF statement.

One can make the argument that the ELSE IF statement first surfaced circa 1977 in FORTRAN 77 and the CASE statement came later in FORTRAN 90 circa 1991. But I know I was using computed GOTOs and GOSUBs long before then.

In any event, to me the computed GOTO is more like the CASE statement than ELSE IF.

Cheers,

tedd

--
-------
http://sperling.com/

--- End Message ---
--- Begin Message ---
I have the following code:

$query = "SELECT * FROM classics";
$result = mysql_query($query);

if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);

for ($j = 0 ; $j < $rows ; ++$j)
{
    $results[] = mysql_fetch_array($result);
}

mysql_close($db_server);

My question, in the loop, why does tha author use:

$results[] = mysql_fetch_array($result);

instead of (as I would expect):

$results[$j] = mysql_fetch_array($result);?

What PHP magic is at work here?

Thanks.


--- End Message ---
--- Begin Message ---
Mike,

$results[] will automatically push a value unto the end of an array.

So doing this...
------
$magic = array();
$magic[] = 'a';
$magic[] = 'b';
$magic[] = 'c';
-----

is exactly this same as doing this...
------
$normal = array();
$normal[0] = 'a';
$normal[1] = 'b';
$normal[2] = 'c';
-----

And yes, in your example "$results[]" would be equivalent to "$results[$j]"


For more reference:
http://www.php.net/manual/en/language.types.array.php


Chris H.


On Sat, Sep 25, 2010 at 4:31 PM, MikeB <[email protected]> wrote:

> I have the following code:
>
> $query = "SELECT * FROM classics";
> $result = mysql_query($query);
>
> if (!$result) die ("Database access failed: " . mysql_error());
> $rows = mysql_num_rows($result);
>
> for ($j = 0 ; $j < $rows ; ++$j)
> {
>    $results[] = mysql_fetch_array($result);
> }
>
> mysql_close($db_server);
>
> My question, in the loop, why does tha author use:
>
> $results[] = mysql_fetch_array($result);
>
> instead of (as I would expect):
>
> $results[$j] = mysql_fetch_array($result);?
>
> What PHP magic is at work here?
>
> Thanks.
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---
--- Begin Message ---
I'd also like to add to that:

$array = array();
$array[] = 'text';
$array[2] = 123;
$array[] = 'hello';

Would output:

$array(
0 => 'text',
2 => 123,
3 => 'hello',
);

Note the missing index 1, as php makes a numerical index that is one greater 
than the highest already in use. As the index 2 was explicitly created, php 
made the next one at 3.

Thanks,
Ash
http://www.ashleysheridan.co.uk

----- Reply message -----
From: "chris h" <[email protected]>
Date: Sat, Sep 25, 2010 22:05
Subject: [PHP] Array question
To: "MikeB" <[email protected]>
Cc: <[email protected]>


Mike,

$results[] will automatically push a value unto the end of an array.

So doing this...
------
$magic = array();
$magic[] = 'a';
$magic[] = 'b';
$magic[] = 'c';
-----

is exactly this same as doing this...
------
$normal = array();
$normal[0] = 'a';
$normal[1] = 'b';
$normal[2] = 'c';
-----

And yes, in your example "$results[]" would be equivalent to "$results[$j]"


For more reference:
http://www.php.net/manual/en/language.types.array.php


Chris H.


On Sat, Sep 25, 2010 at 4:31 PM, MikeB <[email protected]> wrote:

> I have the following code:
>
> $query = "SELECT * FROM classics";
> $result = mysql_query($query);
>
> if (!$result) die ("Database access failed: " . mysql_error());
> $rows = mysql_num_rows($result);
>
> for ($j = 0 ; $j < $rows ; ++$j)
> {
>    $results[] = mysql_fetch_array($result);
> }
>
> mysql_close($db_server);
>
> My question, in the loop, why does tha author use:
>
> $results[] = mysql_fetch_array($result);
>
> instead of (as I would expect):
>
> $results[$j] = mysql_fetch_array($result);?
>
> What PHP magic is at work here?
>
> Thanks.
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---

Reply via email to