Re: [PHP] beginner question about while loops

2004-02-09 Thread Paul Furman
Jochem Maas wrote:
Paul,

   the warning that John refers to does occur - whether you see it 
depends on the error reporting level, PHP will create the array for you 
 if you have not already initialized it but a warning will be generated 
in such cases. ...

error_reporting = E_ALL

...
display_errors = On

...
as a rule of thumb it is better to initialize variable before you use them.

e.g. $pictures = array();

otherwise, in your case, if no files are present in the directory (or 
none have a 'jpg' extension) referencing the $pictures array the while 
loop will cause another error.


Thanks for the advice. I do think the manual is pretty good and I'll 
spend more time with it. But... I do have error reporting set to all. I 
think they may have changed the behavior on newer versions of PHP. I 
have indeed experienced the non-existant array eerror you describe. Lots 
of debugging to do!

PS here's my results from a voracious weekend of coding:

Thanks for all your help!
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] beginner question about while loops

2004-02-09 Thread Jochem Maas
Paul,

   the warning that John refers to does occur - whether you see it 
depends on the error reporting level, PHP will create the array for you 
 if you have not already initialized it but a warning will be generated 
in such cases. - errors that may not display on one machine might be 
visible on another due to differences in the error reporting level.

error reporting level is set in the php.ini file (it can also be set in 
your script see http://nl2.php.net/ini_set), you probably have a line in 
your php.ini that looks like:

error_reporting = E_ALL & ~E_WARNING & ~E_NOTICE

which means: log all errors except warning level and notice level errors
I recommend you try to set the error reporting level to:
error_reporting = E_ALL

in this way all errors are logged (and possibly displayed) - if you can 
write bug/error free code with error reporting set to E_ALL then you 
will have made a good start towards more professional/better coding 
practices.

there is another php.ini line:

display_errors = On

this line determines if errors are output to the browser window. for web 
development this should be definitely on. (productions server often have 
it Off for speed and 'niceness' reasons - i.e. not many users are 
interested in seeing PHP errors, for whatever reason!)

---

as a rule of thumb it is better to initialize variable before you use them.

e.g. $pictures = array();

otherwise, in your case, if no files are present in the directory (or 
none have a 'jpg' extension) referencing the $pictures array the while 
loop will cause another error.

---
Side Note:
for beginners often RTFM only leads to more confusion because the 
contents of many technical manuals often assume that the reader has 
knowledge which a beginner does not yet have. I don't feel that this is 
very much the case of the PHP manual - my feeling is that is very 
accessible. I recommend taking the time to read all of it - the parts 
you don't yet understand often become clear with continued reading. The 
time invested in reading the manual is quickly regained!

Paul Furman wrote:

OK thanks again for helping through the stumbling blocks... I'm rolling 
again now.

John W. Holmes wrote:

Just make sure $pictures is defined as an array before you try to push a
value onto it (even using the method you have now), otherwise you'll 
get a
warning.


It seems to be working fine this way without defining it as an array but 
I guess other languages would not accept that approach.

   while ($file = readdir($dh)){
if (strstr ($file, '.jpg')){
$pictures[] = $file;
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] beginner question about while loops

2004-02-06 Thread John W. Holmes
From: "Paul Furman" <[EMAIL PROTECTED]>

> So when assigning values to an array inside a loop, it knows to advance
> to the next but then if I want to print those out at the same time, it's
> complaining
>
>while ($file = readdir($fh)){
>if (strstr ($file, '.jpg')){
>$pictures[] = $file;
>#print $pictures[];  #Fatal error: Cannot use [] for reading
>}
>var_dump ($pictures[]); #Fatal error: Cannot use [] for reading
>}

Can't do that, like it says. You just assigned $file to $print.. so just
display the value of $file.

---John Holmes...

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



Re: [PHP] beginner question about while loops

2004-02-06 Thread Paul Furman
OK thanks again for helping through the stumbling blocks... I'm rolling 
again now.

John W. Holmes wrote:
Just make sure $pictures is defined as an array before you try to push a
value onto it (even using the method you have now), otherwise you'll get a
warning.
It seems to be working fine this way without defining it as an array but 
I guess other languages would not accept that approach.

   while ($file = readdir($dh)){
if (strstr ($file, '.jpg')){
$pictures[] = $file;
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] beginner question about while loops

2004-02-06 Thread John W. Holmes

- Original Message - 
From: "Paul Furman" <[EMAIL PROTECTED]>

> Totally ignorant need for clarification... Should I set up a counter for
> loops $integer++ or if I'm going through something, the while function
> knows to just go through those and fills in array numbers accordingly?
[snip]
>while ($file = readdir($dh)){
>if (strstr ($file, '.jpg')){
>$pictures[] = $file;
>}

With this syntax, you do not need to keep an $integer variable for the array
key. When you use $array[] = "something", it just assigns that value to the
next available numeric key.

while ($file = readdir($dh)){
if (strstr ($file, '.jpg')){
$pictures[$int++] = $file;
}

Would also be correct, if you wanted to keep the count yourself. You could
also use array_push().

Just make sure $pictures is defined as an array before you try to push a
value onto it (even using the method you have now), otherwise you'll get a
warning.

---John Holmes...

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



RE: [PHP] beginner question about while loops

2004-02-06 Thread Shaunak Kashyap
1. Using [] creates a new array element. Hence the error. You can try this
piece of code inside the loop

[code]
if (strstr ($file, '.jpg')){

$refPictures = & $pictures[];

$refPictures = $file;
  print $refPictures;
}
[/code]

$refPictures holds a reference to the newly created element of the $pictures
array. Therefore, by assigning $file to $refPictures, $file is actually
getting assigned to the newly created element of the $pictures array. The
same logic applies in the print statement

2. Again, using [] in the var_dump indicates that you are trying to create a
new element of the $pictures array. If dumping the contents of the entire
array along with their data types and such is what you are trying to
achieve, the correct syntax is

[code]
var_dump($pictures);
[/code]

> -Original Message-
> From: Paul Furman [mailto:[EMAIL PROTECTED]
> Sent: Friday, February 06, 2004 3:09 PM
> To: [EMAIL PROTECTED]
> Subject: Re: [PHP] beginner question about while loops
>
>
> Eric Gorr wrote:
> >
> >> the while function knows to just go through those and fills in array
> >> numbers accordingly?
> >
> >
> > The while function has nothing to do with it. Using the syntax $array[]
> > simply adds an element onto the _end_ of the array and PHP picks the
> > next logical, numerical index.
>
>
> OK thanks guys, I got the missing curly brace & some other messes.
>
> So when assigning values to an array inside a loop, it knows to advance
> to the next but then if I want to print those out at the same time, it's
> complaining
>
>while ($file = readdir($fh)){
>if (strstr ($file, '.jpg')){
>$pictures[] = $file;
>#print $pictures[];  #Fatal error: Cannot use [] for reading
>}
>var_dump ($pictures[]); #Fatal error: Cannot use [] for reading
>}
>
>
>
> This one works but complains about Undefined variable: pictures NULL
> array (but it dumps the contents of $pictures[]:
>
>while ($file = readdir($fh)){
>if (strstr ($file, '.jpg')){
>$pictures[] = $file;
>}
>var_dump ($pictures);
>}
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

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



Re: [PHP] beginner question about while loops

2004-02-06 Thread Rob Adams

"Paul Furman" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Eric Gorr wrote:

[snip]

>
>while ($file = readdir($fh)){
>if (strstr ($file, '.jpg')){
>$pictures[] = $file;
>#print $pictures[];  #Fatal error: Cannot use [] for reading

Which element are you trying to print?  I'm guessing the current one, so
just use:
  print $file;

>}
>var_dump ($pictures[]); #Fatal error: Cannot use [] for reading
>}
>
>
>
> This one works but complains about Undefined variable: pictures NULL
> array (but it dumps the contents of $pictures[]:
>
>while ($file = readdir($fh)){
>if (strstr ($file, '.jpg')){
>$pictures[] = $file;
>}
>var_dump ($pictures);
>}

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



Re: [PHP] beginner question about while loops

2004-02-06 Thread Paul Furman
Eric Gorr wrote:

the while function knows to just go through those and fills in array 
numbers accordingly?


The while function has nothing to do with it. Using the syntax $array[] 
simply adds an element onto the _end_ of the array and PHP picks the 
next logical, numerical index.


OK thanks guys, I got the missing curly brace & some other messes.

So when assigning values to an array inside a loop, it knows to advance 
to the next but then if I want to print those out at the same time, it's 
complaining

  while ($file = readdir($fh)){
  if (strstr ($file, '.jpg')){
  $pictures[] = $file;
  #print $pictures[];  #Fatal error: Cannot use [] for reading
  }
  var_dump ($pictures[]); #Fatal error: Cannot use [] for reading
  }


This one works but complains about Undefined variable: pictures NULL 
array (but it dumps the contents of $pictures[]:

  while ($file = readdir($fh)){
  if (strstr ($file, '.jpg')){
  $pictures[] = $file;
  }
  var_dump ($pictures);
  }
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


RE: [PHP] beginner question about while loops

2004-02-06 Thread Alex Hogan
You didn't close your loop;

while ($file = readdir($dh)){
if (strstr ($file, '.jpg')){
$pictures[] = $file;
}
#vardump ($pictures[]);
} <= here
 ?>

instead of;

while ($file = readdir($dh)){
if (strstr ($file, '.jpg')){
$pictures[] = $file;
}
#vardump ($pictures[]);
 ?>

alex

> -Original Message-
> From: Paul Furman [mailto:[EMAIL PROTECTED]
> Sent: Friday, February 06, 2004 1:41 PM
> To: [EMAIL PROTECTED]
> Subject: [PHP] beginner question about while loops
> 
> Totally ignorant need for clarification... Should I set up a counter for
> loops $integer++ or if I'm going through something, the while function
> knows to just go through those and fills in array numbers accordingly?
> 
> Below is my project (while loop at the bottom):
> 
> I'm getting unexpected $end on line 18 but the file only has 17 lines!
> 
> if (isset ($_REQUEST ['IMGDIR'])){
>$imagedir=$_REQUEST ['IMG_DIR'];
> } else {
>$imagedir = PUB_DIR . '/grasses';
> #PUB_DIR coordinates my public htm location at home & school
> }
>if (!(chdir ($imagedir))){
>  print "invalid directory";
>  }
>$fh=opendir($imagedir);
>while ($file = readdir($dh)){
>if (strstr ($file, '.jpg')){
>$pictures[] = $file;
>}
>#vardump ($pictures[]);
> ?>
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php


** 
The contents of this e-mail and any files transmitted with it are 
confidential and intended solely for the use of the individual or 
entity to whom it is addressed.  The views stated herein do not 
necessarily represent the view of the company.  If you are not the 
intended recipient of this e-mail you may not copy, forward, 
disclose, or otherwise use it or any part of it in any form 
whatsoever.  If you have received this e-mail in error please 
e-mail the sender. 
** 




Re: [PHP] beginner question about while loops

2004-02-06 Thread Eric Gorr
At 11:41 AM -0800 2/6/04, Paul Furman wrote:
  while ($file = readdir($dh)){
  if (strstr ($file, '.jpg')){
  $pictures[] = $file;
  }
Spotted this problem when staring at your code.

Number of open braces: 2
Number of close braces: 1
You need to close off your while loop.

Should I set up a counter for loops $integer++ or if I'm going 
through something
You can if you like, but it is not necessary in this case.

the while function knows to just go through those and fills in array 
numbers accordingly?
The while function has nothing to do with it. Using the syntex 
$array[] simply adds an element onto the _end_ of the array and PHP 
picks the next logical, numerical index.

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


[PHP] beginner question about while loops

2004-02-06 Thread Paul Furman
Totally ignorant need for clarification... Should I set up a counter for 
loops $integer++ or if I'm going through something, the while function 
knows to just go through those and fills in array numbers accordingly?

Below is my project (while loop at the bottom):

I'm getting unexpected $end on line 18 but the file only has 17 lines!


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