Re: [PHP] Question - foreach.

2010-06-11 Thread tedd

At 3:46 PM -0400 6/10/10, Paul M Foster wrote:

On Thu, Jun 10, 2010 at 11:16:08AM -0400, tedd wrote:

  I spend much of my time thinking Did I do that before?

grin I know the feeling. I will say this, though. I have yet to figure
out, from your URLs, how your site(s) is/are organized. Maybe a reorg
would help?

Paul


Paul:

Unfortunately, I really don't follow an organization plan for my 
demos on any of my sites (well over a dozen now).


Please understand that when I started creating demos, I only wanted 
to see how a specific thing worked. I had no idea that this 
investigation would become a giant listing of stuff.


I could explain how I can easily create demos if you want, but it's 
pretty basic stuff using includes for a common header/footer files 
leaving only the specific of the topic to be added. The hard part is 
just finding a layout that you like -- after that it's pretty easy to 
duplicate it each time you want to demo something.


I will be updating my sperling.com soon to add in language specific 
code (php/css/js) -- and that *will* be organized into categories. 
However, that may be down the road because I have a few other 
pressing matters that are pulling me in several different directions.


Cheers,

tedd

--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Question - foreach.

2010-06-10 Thread tedd

At 7:19 AM +0530 6/10/10, Shreyas wrote:

PHP'ers,

I am reading a PHP book which explains foreach and at the end says : *'When
foreach starts walking through an array, it moves the pointer to
the beginning of the array. You don't need to reset an array before
walking through it with foreach.'*
*
*
*Does this mean - *
*1) Before I navigate the array, foreach will bring the pointer to the
starting key?*
*2) After the first index, it goes to 2nd, 3rd, and nth? *


Regards,
Shreyas


Shreyas:

This is one of those questions that you can test very easily, just 
initialize an array and try it.


?php

$test = array(a, b, c, d);
foreach ($test as $value)
   {
   echo(value = $value br);
   }

?

As the references show, there are two versions of the foreach, the 
one above and this:


?php

$test = array(a, b, c, d);
foreach ($test as $key = $value)
   {
   echo($key= $key  value=$value br);
   }

?

Note that you can pull-out the index (i.e., $key) as well as the 
value (i.e., $value) of each index. The br is only to add a 
linefeed in html.


This is a bit easier than using a for() loop.

Cheers,

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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Question - foreach.

2010-06-10 Thread Paul M Foster
On Thu, Jun 10, 2010 at 07:03:28AM -0400, tedd wrote:

 At 7:19 AM +0530 6/10/10, Shreyas wrote:
 PHP'ers,

 I am reading a PHP book which explains foreach and at the end says : *'When
 foreach starts walking through an array, it moves the pointer to
 the beginning of the array. You don't need to reset an array before
 walking through it with foreach.'*
 *
 *
 *Does this mean - *
 *1) Before I navigate the array, foreach will bring the pointer to the
 starting key?*
 *2) After the first index, it goes to 2nd, 3rd, and nth? *


 Regards,
 Shreyas

 Shreyas:

 This is one of those questions that you can test very easily, just
 initialize an array and try it.

+1

This is Tedd's modus operandi. His website(s) are full of exactly this
type of thing. And I have to agree. I can't count the number of
questions I *haven't* asked on this list, because I built a page to test
a particular concept. And this sort of activity (as opposed to just
reading about something) really locks in your understanding of a
concept.

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Question - foreach.

2010-06-10 Thread Shreyas
All,

I tried and tested it but wanted a solid confirmation on it. I felt foreach
usage is better than manual way of next(), prev() et al.

Thanks for the comments. I consider the thread answered and solved unless
someone has anything more to add.

Regards,
Shreyas

On Thu, Jun 10, 2010 at 7:02 PM, Paul M Foster pa...@quillandmouse.comwrote:

 On Thu, Jun 10, 2010 at 07:03:28AM -0400, tedd wrote:

  At 7:19 AM +0530 6/10/10, Shreyas wrote:
  PHP'ers,
 
  I am reading a PHP book which explains foreach and at the end says :
 *'When
  foreach starts walking through an array, it moves the pointer to
  the beginning of the array. You don't need to reset an array before
  walking through it with foreach.'*
  *
  *
  *Does this mean - *
  *1) Before I navigate the array, foreach will bring the pointer to the
  starting key?*
  *2) After the first index, it goes to 2nd, 3rd, and nth? *
 
 
  Regards,
  Shreyas
 
  Shreyas:
 
  This is one of those questions that you can test very easily, just
  initialize an array and try it.

 +1

 This is Tedd's modus operandi. His website(s) are full of exactly this
 type of thing. And I have to agree. I can't count the number of
 questions I *haven't* asked on this list, because I built a page to test
 a particular concept. And this sort of activity (as opposed to just
 reading about something) really locks in your understanding of a
 concept.

 Paul

 --
 Paul M. Foster

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Regards,
Shreyas


Re: [PHP] Question - foreach.

2010-06-10 Thread tedd

At 9:32 AM -0400 6/10/10, Paul M Foster wrote:

On Thu, Jun 10, 2010 at 07:03:28AM -0400, tedd wrote:

  This is one of those questions that you can test very easily, just

 initialize an array and try it.


+1

This is Tedd's modus operandi. His website(s) are full of exactly this
type of thing. And I have to agree. I can't count the number of
questions I *haven't* asked on this list, because I built a page to test
a particular concept. And this sort of activity (as opposed to just
reading about something) really locks in your understanding of a
concept.

Paul


Paul:

Now, if I could get the old memory to lock in and remember it, it 
would be great!


I spend much of my time thinking Did I do that before?

Cheers,

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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Question - foreach.

2010-06-10 Thread Paul M Foster
On Thu, Jun 10, 2010 at 11:16:08AM -0400, tedd wrote:

 At 9:32 AM -0400 6/10/10, Paul M Foster wrote:
 On Thu, Jun 10, 2010 at 07:03:28AM -0400, tedd wrote:

   This is one of those questions that you can test very easily, just
  initialize an array and try it.

 +1

 This is Tedd's modus operandi. His website(s) are full of exactly this
 type of thing. And I have to agree. I can't count the number of
 questions I *haven't* asked on this list, because I built a page to test
 a particular concept. And this sort of activity (as opposed to just
 reading about something) really locks in your understanding of a
 concept.

 Paul

 Paul:

 Now, if I could get the old memory to lock in and remember it, it
 would be great!

 I spend much of my time thinking Did I do that before?

grin I know the feeling. I will say this, though. I have yet to figure
out, from your URLs, how your site(s) is/are organized. Maybe a reorg
would help?

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Question - foreach.

2010-06-10 Thread Bob McConnell
From: Paul M Foster

 On Thu, Jun 10, 2010 at 11:16:08AM -0400, tedd wrote:

 At 9:32 AM -0400 6/10/10, Paul M Foster wrote:
 On Thu, Jun 10, 2010 at 07:03:28AM -0400, tedd wrote:


 Paul:

 Now, if I could get the old memory to lock in and remember it, it
 would be great!

 I spend much of my time thinking Did I do that before?
 
 grin I know the feeling. I will say this, though. I have yet to
figure
 out, from your URLs, how your site(s) is/are organized. Maybe a reorg
 would help?

ISTR there are three signs of old age. The first is loss of memory, but
I can never remember the other two.

Bob McConnell

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Question - foreach.

2010-06-10 Thread David McGlone
On Thursday 10 June 2010 11:16:08 tedd wrote:
 At 9:32 AM -0400 6/10/10, Paul M Foster wrote:
 On Thu, Jun 10, 2010 at 07:03:28AM -0400, tedd wrote:
This is one of those questions that you can test very easily, just
   
   initialize an array and try it.
 
 +1
 
 This is Tedd's modus operandi. His website(s) are full of exactly this
 type of thing. And I have to agree. I can't count the number of
 questions I *haven't* asked on this list, because I built a page to test
 a particular concept. And this sort of activity (as opposed to just
 reading about something) really locks in your understanding of a
 concept.
 
 Paul
 
 Paul:
 
 Now, if I could get the old memory to lock in and remember it, it
 would be great!
 
 I spend much of my time thinking Did I do that before?

Looks like you and I are in the same boat! My memory these days has went to 
the dumps.

Although I do the same thing Paul does to actually grasp a more in depth 
understanding of something, sometimes in a day or two it's often forgotten.

-- 
Blessings,
David M.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Question - foreach.

2010-06-09 Thread Shreyas
PHP'ers,

I am reading a PHP book which explains foreach and at the end says : *'When
foreach starts walking through an array, it moves the pointer to
the beginning of the array. You don’t need to reset an array before
walking through it with foreach.'*
*
*
*Does this mean - *
*1) Before I navigate the array, foreach will bring the pointer to the
starting key?*
*2) After the first index, it goes to 2nd, 3rd, and nth? *


Regards,
Shreyas


Re: [PHP] Question - foreach.

2010-06-09 Thread Adam Richardson
On Wed, Jun 9, 2010 at 9:49 PM, Shreyas shreya...@gmail.com wrote:

 PHP'ers,

 I am reading a PHP book which explains foreach and at the end says : *'When
 foreach starts walking through an array, it moves the pointer to
 the beginning of the array. You don’t need to reset an array before
 walking through it with foreach.'*
 *
 *
 *Does this mean - *
 *1) Before I navigate the array, foreach will bring the pointer to the
 starting key?*
 *2) After the first index, it goes to 2nd, 3rd, and nth? *


 Regards,
 Shreyas


Number 1.

Adam

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


Re: [PHP] Question - foreach.

2010-06-09 Thread Jim Lucas

Shreyas wrote:

PHP'ers,

I am reading a PHP book which explains foreach and at the end says : *'When
foreach starts walking through an array, it moves the pointer to
the beginning of the array. You don’t need to reset an array before
walking through it with foreach.'*
*
*
*Does this mean - *
*1) Before I navigate the array, foreach will bring the pointer to the
starting key?*
*2) After the first index, it goes to 2nd, 3rd, and nth? *


Regards,
Shreyas



Here is your best reference:

http://php.net/foreach

Look at the two Notes sections on the top of the page.

The first says this:

Note: When foreach first starts executing, the internal array pointer is 
automatically reset to the first element of the array. This means that 
you do not need to call reset()  before a foreach  loop.


Basically what you said.  But then the second says this

Note: Unless the array is referenced, foreach operates on a copy of the 
specified array and not the array itself. foreach  has some side effects 
on the array pointer. Don't rely on the array pointer during or after 
the foreach without resetting it.


--
Jim Lucas

A: Maybe because some people are too annoyed by top-posting.
Q: Why do I not get an answer to my question(s)?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Question - foreach.

2010-06-09 Thread Daniel Brown
On Wed, Jun 9, 2010 at 21:49, Shreyas shreya...@gmail.com wrote:
 PHP'ers,

 I am reading a PHP book which explains foreach and at the end says : *'When
 foreach starts walking through an array, it moves the pointer to
 the beginning of the array. You don’t need to reset an array before
 walking through it with foreach.'*
 *
 *
 *Does this mean - *
[snip!]

An easy way to think about it: foreach is cocky and doesn't give a
damn about the rules array functions or placements have set in place.
It'll start from the beginning, and to hell with everyone else.

In other words: foreach will iterate wholly; it will count *for*
*each* key in the loop, not just where another portion of the code
left off.

-- 
/Daniel P. Brown
daniel.br...@parasane.net || danbr...@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
We now offer SAME-DAY SETUP on a new line of servers!

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php