Re: Re: Pre/Post inc (Was array conversion)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, Feb 20, 2010 at 11:10 AM, Nathan Rixham <nrixham@xxxxxxxxx> wrote:

> Richard Quadling wrote:
> > On 20 February 2010 11:18,  <clancy_1@xxxxxxxxxxxx> wrote:
> >> Or:
> >>
> >> $a = array ('Cats', 'white', 'Dogs', 'black', 'Mice', 'grey', 'Camels',
> 'brown');
> >> $b = '';                                // Just in case it has some
> leftover value
> >> $k = 2* (int) (count ($a)/2);   // ensure even no of terms
> >> $i = 0; while ($i < $k)
> >>        {
> >>        $b[$a[$i++]] = $a[$i++];  // ***
> >>        }
> >>
> >> And this works:
> >> $i = 0; $k = array_keys($b);
> >> while ($i < count($b)) {        echo '<h5>'.$i.': '.$k[$i].' = '.
> $b[$k[$i++]].'</h5>'; }
> >>
> >> 0: Cats = white
> >> 1: Dogs = black
> >> 2: Mice = grey
> >> 3: Camels = brown
> >>
> >> ( *** I have always been wary of using statements like this because I
> was unsure when the
> >> incrementing would occur, so I tried it.)
> >>
> >> Clancy
> >
> >
> > <?php
> > $i = 10;
> > echo $i++; // Shows 10 and $i becomes 11
> > echo ++$i; // $i becomes 12 and 12 is shown.
> > ?>
> >
> > Post increment and pre increment.
> >
> > No need to be "wary" of them.
> >
> > http://docs.php.net/manual/en/language.operators.increment.php
> >
>
> Expanding on what Richard says; there does seem to be a growing number
> of people who haven't stopped to learn the very basics of PHP (or
> languages in general).
>
> I'd strongly recommend that all those in doubt over the basics take a
> few hours out to (re-)familiarise themselves; and there's no finer
> resource to do this than the php manual [1]
>
> You'll notice the manual goes as follows:
> # Basic syntax
> # Types
> # Variables
> # Constants
> # Expressions
> # Operators
> # Control Structures
> # Functions
> ... more
>
> And that's the order in which you should learn; in short you can't
> really program or script without knowing basics through control structures.
>
> Do hope this mail doesn't sound condescending in any way; as it's meant
> with the best intentions and really will make you're (working) life a
> lot easier. I myself still refer back to these base sections
> periodically, and every time I do - a new detail pops out that makes
> something easier.
>
> [1] http://docs.php.net/manual/en/langref.php
>
> Many Regards,
>
> Nathan
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Clarifying what Clancy specifically said:

>> ( *** I have always been wary of using statements like this because I was
unsure when the
>> incrementing would occur, so I tried it.)

The asterisks inform us he was speaking about the first example, and the
language informs us he was speaking about the statement, not the increment
operator, itself.

>> $b[$a[$i++]] = $a[$i++];  // ***

It's actually quite a clever implementation of the algorithm request that
prompted this thread, and because of it's uniqueness, (incrementing a
variable multiple times within the same statement on both sides of the '='),
I don't this reflects that Clancy "stopped to learn the very basics of PHP."
 Actually, Clancy taking the time to try something to better learn the
language (just for the sake of coding fun, no less) reflects the desire to
learn new things, and that's exactly the type of person I hope is drawn to
the PHP community.

Thanks for the example, Clancy :)

Adam

-- 
Nephtali:  PHP web framework that functions beautifully
http://nephtaliproject.com

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux