php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #6507 in_array never returns a proper value
Submitted: 2000-09-03 00:08 UTC Modified: 2000-09-16 17:55 UTC
From: darron at froese dot org Assigned:
Status: Closed Package: *General Issues
PHP Version: 4.0.2 OS: Linux (Mandrake 7.0, 7.1)
Private report: No CVE-ID: None
 [2000-09-03 00:08 UTC] darron at froese dot org
This script works just fine in PHP 4.0.0 but fails in 4.0.1pl2, 4.0.2, PHP from CVS as of 8PM 09/02/2000.

It's online and works here:
PHP 4.0.0 <http://www.gary-campbell.com/test.php>
Mandrake 7.0, Apache 1.3.12, Compiled as a DSO.

It fails on this machine:
PHP 4.0.2-cvs <http://www.educ8r.net/~darron/test.php>
Mandrake 7.1, Apache 1.3.12, Compiled as a DSO.

I tried it on a machine just now with the CVS version of PHP and it still fails. I've also tried it on a 4.0.2-release machine - it fails there too.

<?

for ($counter = 0; $counter < 11; $counter++) {
        $items[$counter] = $counter;
}

for ($counter = 0; $counter < 11; $counter++) {
        print "$counter = $items[$counter]<BR>\n";
}

print "<P>";

for ($counter = 0; $counter < 11; $counter++) {   
        print "$counter = $items[$counter]<BR>\n";
        
        if (in_array("$counter", $items)) {
                $current_item = current($items);
                $previous_item = prev($items);
                print "Current: $current_item<BR>\n";  
                print "Previous: $previous_item<BR>\n";
        }
        
        if (in_array("$counter", $items)) {
                $next_item = next($items);
                print "Next: $next_item<BR>\n";
        }
        
        if ($prev_item) {
                print "Prev Item Exists<BR>\n";
        } else {
                print "Prev Item Does NOT Exist<BR>\n";
        }
        
        if ($next_item) {
                print "Next Item Exists<BR>\n";
        } else {
                print "Next Item Does NOT Exist<P>\n";
        }
} 
  
?>

The php-4.0.0 was compiled with:

./configure \
--with-apxs=/usr/local/apache/bin/apxs \
--with-mysql=/usr/local/mysql-prod \
--with-pdflib \
--enable-ftp \
--with-ldap \
--with-imap=../imap-4.5 \
--with-zlib \
--with-jpeg-dir=/usr/lib \
--with-tiff-dir=/usr/lib \
--with-zlib-dir=/usr/lib \
--with-png-dir=/usr/lib \
--with-gd \
--with-ttf \
--with-xml \
--enable-trans-sid \
--enable-track-vars=yes \
--enable-magic-quotes=yes

The php-4.0.2 was compiled with:

./configure \
--with-apxs=/usr/local/apache/bin/apxs \
--with-mysql=/usr/local/mysql-prod \
--with-pdflib \
--enable-ftp \
--with-ldap \
--with-imap=../imap-4.5 \
--with-zlib \
--with-jpeg-dir=/usr/lib \
--with-tiff-dir=/usr/lib \
--with-zlib-dir=/usr/lib \
--with-png-dir=/usr/lib \
--with-gd \
--with-ttf \
--with-xml \
--enable-trans-sid \
--enable-track-vars=yes \
--enable-magic-quotes=yes

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-09-03 00:24 UTC] darron at froese dot org
Sorry, the description is wrong - in_array still seems to be working, it's current($array), prev($array) and next($array) that seem to be misbehaving.
 [2000-09-03 06:01 UTC] stas@php.net
before using next, perv and current, you should do reset(). Then, next and prev not only get items, but move array pointer. Please read the manual about that.
 [2000-09-03 14:44 UTC] darron at froese dot org
> before using next, perv and current, you should do reset(). > Then, next and prev not only get items, but move array
> pointer. Please read the manual about that.

That's the behaviour you see in php 4.0.0 and the behaviour I'm used to (that's why there's two times I do "if (in_array("$counter", $items))"). It doesn't work in anything since then.

This code works just fine in PHP 4.0.0 *with* or *without* the reset($items) - it's online and working at

<http://gary-campbell.com/test.php>

This code fails under any version of PHP since then on multiple machines. It's online and failing at

<http://www.educ8r.net/~darron/test3.php>

Here's the code:

<?

for ($counter = 1; $counter < 11; $counter++) {
        $items[$counter] = $counter;
}

for ($counter = 1; $counter < 11; $counter++) {
        print "$counter = $items[$counter]<BR>\n";
}

print "<P>";

for ($counter = 1; $counter < 11; $counter++) {
        print "$counter = $items[$counter]<BR>\n";

        reset($items);
        
        if (in_array("$counter", $items)) {
                $current_item = current($items);
                $previous_item = prev($items);
                print "Current: $current_item<BR>\n";  
                print "Previous: $previous_item<BR>\n";
        }
        
        if (in_array("$counter", $items)) {
                $next_item = next($items);
                print "Next: $next_item<BR>\n";
        }
        
        if ($previous_item) {
                print "Prev Item Exists<BR>\n";
        } else {
                print "Prev Item Does NOT Exist<BR>\n";
        }
        
        if ($next_item) {
                print "Next Item Exists<P>\n";
        } else {
                print "Next Item Does NOT Exist<P>\n";
        }
} 
  
?>
 [2000-09-16 10:10 UTC] darron at froese dot org
I tried this again on another server - it's a Mandrake 7.0 box. I recompiled Apache with this configure line:

./configure \
--prefix=/usr/local/apache \
--enable-module=so

Then I compiled php-4.0.0 with this line and tested:

./configure --with-apxs=/usr/local/apache/bin/apxs

The code that I posted before worked just fine with php-4.0.0. It's been posted here:

<http://gateway.javelindigital.com/test.phps>
<http://gateway.javelindigital.com/test.php>

I just grabbed the CVS code (8PM MST 09/16/2000) for php4 and recompiled the module with this configure line:

./configure --with-apxs=/usr/local/apache/bin/apxs

Now that I've upgraded to 4.0.3-dev, the code doesn't work.

This is the 4th machine I've tested this on:

<http://froese.org/~darron/test.php>
<http://www.gary-campbell.com/test.php>
<http://www.educ8r.net/~darron/test.php>
<http://gateway.javelindigital.com/test.php>

Only the machine with php-4.0.0 works - the rest all fail.
 [2000-09-16 17:55 UTC] stas@php.net
When you use prev() just after reset(), you get no value - that's OK, because reset() makes array pointer to point to the first element.
You cannot use next() after that too, without reset(), because array pointer is not valid anymore. You need to check that prev() failed and do the reset().
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 03:01:33 2024 UTC