Date: 10/02/00
- Next message: Josh Sisk: "[phplib] Garbage Collection/Sessions"
- Previous message: andRie Is...: "Re: [phplib] Connecting to oracle 8"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Rex,
Yes. As I recall from a discussion from the PHP mailing list a while ago,
it works like this:
When PHP encounters a "naked" string (i.e., with no quotes), it first
tries to see if it's a built-in word, then checks if it's a constant. If
neither of those is true, it tries to make it a string.
So to avoid mistakes, you should always dress up your strings like
strings, whether they are array keys or function arguments or whatever.
You can try setting
$array[if] = 'foo'
versus
$array['if'] = 'foo'
and see what happens. Aside from the possible confusion of the PHP
parser, you run a slight performance deficit each time, whereas if you
told PHP it was a string all along, it won't have to guess.
--Matt
----------------------------------------------------------------
Matthew Leingang http://www.math.rutgers.edu/
Rutgers University leingang <email protected>
Department of Mathematics "This signature needs no quote."
On Mon, 2 Oct 2000, Rex Byrns wrote:
> does this same advice apply to the use of phplib?
>
> i.e. - $db->f(ID) vs. $db->f('ID') ?
>
> Thanks for the advice.
>
>
> -----Original Message-----
> From: Matthew Leingang [mailto:leingang <email protected>]
> Sent: Saturday, September 30, 2000 4:42 PM
> To: Holger Blasum
> Cc: Rex Byrns; Phplib <email protected> Netuse. De (E-mail)
> Subject: Re: [phplib] Array Insanity
>
>
> Hello!
>
> May I add (Rex) that your associative array keys (Parent, Name, etc...)
> should be quoted so the parser won't confuse them with constants or
> reserved words.
>
> ----------------------------------------------------------------
> Matthew Leingang http://www.math.rutgers.edu/
> Rutgers University leingang <email protected>
> Department of Mathematics "This signature needs no quote."
>
> On Sat, 30 Sep 2000, Holger Blasum wrote:
>
> > On Sat, Sep 30, 2000 at 12:13:44AM +0200, Rex Byrns wrote:
> >
> > Rex:
> >
> > [some code to create an associative array from a database query]
> >
> > > $garray = array();
> > (superfluous)
> > > $garray = array($newarray);
> > (add a comment here what garray is, so you will remember next month :) )
> > > $c = count($newarray);
> > > echo $c." elements in newarray<BR>";
> > > $i = 0;
> > > $j = 0;
> > >
> > > while ($i <= $c){
> > $i < $c
> > > $parent = $newarray[$i][Parent];
> > > $na = $newarray[$i][Name];
> > > #echo $na;
> > > reset($garray);
> > > $gcount = count($garray);
> > > $j = 1;
> > $j = 0;
> > >
> > > While ($j <= $gcount){
> > Here you intended: while $j < $gcount. As in many other languages, array
> indexing starts with 0. This only does not crash because assoc arrays
> contains their indices in php.
> > > if($garray[$j][ID]=$parent){
> >
> > Here you intended to write if($garray[$j][ID] == $parent) {.
> > As is, with the single equality sign, this line *always* will evaluate to
> true in each run. So it is true in the last run (where $j is $gcount).
> Consequently
> > the entries of the $garray[$gcount] are always used.
> >
> > > $newarray[$i][Used]='1';
> > > $newarray[$i][Pname]= $garray[$j][Name];
> > > }
> > > $j++;
> > > }
> > > $i++;
> > > }
> > >
> >
> > Using the count-for loop like this on an associatiave array will also run
> through the indices of the associative array. You can easily gain insight
> into the structure of an associative array by introspecting it using the
> "echo serialize ($array);" command. (Playing with serialize is any case
> highly recommendable for understanding PHP data structures.)
> >
> > The *much* more common approach for traversing through associative arrays
> is to use the "while (($key, $value) = each ($array))" construct (no need
> for counting, for more extensive doc see the miscellaneous each ()
> function).
> >
> > BTW, that code is not very efficient for large arrays, because if you have
> eg 10000 entries you can expect to run through it 10000*10000/2 times. Iff
> that is an issue think of checking using an associative array (whose
> ordering is automatically optimized for you) directly for the parent-child
> relationship so you only will have to write "if
> (isset($array_a[$array_b[$parent]])". Umm, if you are using tree
> relationships anyhow, there is a PHPLIB tree module (tree.inc) which may be
> worth looking at.
> >
> > HTH (admittedly I found the oddities of PHP associative arrays once hard
> to understand too...) , Holger
> >
> > Pls sign http://petition.eurolinux.org against software patents in Europe.
> >
> > [ Part 11: "Included Message" ]
> >
>
>
>
> [ Part 7: "Included Message" ]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: phplib-unsubscribe <email protected>
For additional commands, e-mail: phplib-help <email protected>
- Next message: Josh Sisk: "[phplib] Garbage Collection/Sessions"
- Previous message: andRie Is...: "Re: [phplib] Connecting to oracle 8"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

