php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #10026 For loop always execute
Submitted: 2001-03-27 11:07 UTC Modified: 2001-07-09 07:54 UTC
From: svein at edbc dot no Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.4pl1 OS: RH Linux 6.1
Private report: No CVE-ID: None
 [2001-03-27 11:07 UTC] svein at edbc dot no
I have a strange situation;
I have this code : 

$c = count($myarray);
echo $c . '<br>';

for ($i=0; $i<$c; $i++) { 
   code goes here... 
}

echo often often output that $c is 0 BUT the for loop ALWAYS execute...
I also have tried to sorrund the for loop with this :
if ($c>0) {
  for(...)
}

and it STILL execute if $c==0

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-03-27 11:09 UTC] svein at edbc dot no
The code submitted is inside a function that is called recursively and its not exact copy of the code.

I've also tried to change the for loop to a while loop with the same result.
 [2001-03-29 11:55 UTC] sniper@php.net
I can't reproduce this (using code below):

<?php

$c=0;

for($i=0; $i<$c; $i++) {
    echo "yes\n";
}

?>

Could you try this one?
And note: If the variable passed to count() is not an array,
it will return 1.

--Jani

 [2001-03-29 12:55 UTC] svein at edbc dot no
Its not easy reproduceable - i have only seen this happens ONCE - in the specified function. Its NOT a simple for-loop - the for-loop executes INSIDE a recursively called function.

I've also tried this one :
---
$c = count($myarray);
if ($c>0) {
  echo "c is greater than 0<br>";
  for (...) {
    ...code
  }
}
---
then IF c is 0 the echo statement doesnt execute BUT the for loop executes...

I cant figure out why...

If some of you want it i can mail you the file so you can see the things in perspective.

- Svein
 [2001-03-29 12:57 UTC] svein at edbc dot no
When i said ONCE - i didnt mean ONCE :)
It ALWAYS happen in the code referred...
 [2001-04-01 13:34 UTC] sniper@php.net
As I can not reproduce this with latest CVS please try
the latest snapshot from http://snaps.php.net/

And if happens with it too, create one 
short but self-containing script which can be used
to reproduce this anywhere without modifications
and add it into this bug report.

--Jani

 [2001-04-04 06:24 UTC] svein at edbc dot no
I post the function that cause the problem.
Its the for-loop int the wery bottom of the function that cause my problem. If you can see any wrong logic in the code please let me know :)

I have solved this problem in another way in my current code. 
The code below have worked for a long time, but stopped working and caused described problem after installing - i think php4.0.4 or 4.0.4pl1

Even if i surrond the for-loop with this :

echo "<br>First : $c";
if ($c!=0) {
  echo "<br>Second : $c";
  for (..) {
    echo "<br>Inside for-loop...";
    ...
  }
}

the first echo output 0 but the second echo output another value like 1 or 2.
The strange thing is that if the FIRST echo output 0 the SECOND echo doesnt execute **BUT** the for-loop execute!



  // Parse the parts of message and get info on each
  function parseparts($ref, $ofs='') {
    global $part_no, $msg_part, $imap, $sorted_msgs, $msg_no, $bgcolor, $background, $folder;
    global $sort_order;
 
    $part_no++;
        
    $msg_part[$part_no] = $ref;
    $msg_part[$part_no]->index = $ofs;

    $ac = count($ref->dparameters);
    for ($i=0; $i<$ac; $i++)
		if (eregi($ref->dparameters[$i]->attribute, 'filename')) {
			$msg_part[$part_no]->filename = $ref->dparameters[$i]->value;
			break;
		};
    
    $ac = count($ref->parameters);
    for ($i=0; $i<$ac; $i++)
      if (eregi($ref->parameters[$i]->attribute, 'name')) {
          $msg_part[$part_no]->name = $ref->parameters[$i]->value;
          break;
        };
    
    if (!strcmp($msg_part[$part_no]->subtype, 'HTML')) { // Get background for HTML message
      $tmp = @imap_fetchbody($imap, $sorted_msgs[$msg_no-1], $msg_part[$part_no]->index);
      // Encode data
      switch($msg_part[$part_no]->encoding) {
      	case ENCBASE64  : $tmp = @imap_base64($tmp); break;
      	case ENCQUOTEDPRINTABLE : $tmp = @imap_qprint($tmp); break;
      };

      // Background color
      if (preg_match('/<BODY.*?BGCOLOR=([^\s]+)>/si', $tmp, $tmp2)) {
        $bgcolor=$tmp2[1];
        $bgcolor = 'BGCOLOR="'.preg_replace('/\"/', '', $bgcolor).'"';
      } else
        $bgcolor='BGCOLOR="#ffffff"';;
      // Remove any "

      // Background image
      if (preg_match('/<BODY.*?BACKGROUND=([^\s]+)>/si', $tmp, $tmp2)) 
        $tmp=$tmp2[1];
      else
        $tmp='';

      if (!empty($tmp)) {
        $background = "background=\"getpart.phtml?1get_type=1&part_id=".rawurlencode(ereg_replace("\"", '', $tmp))."\"";
      } else $background = '';

    };  
    
    $c = count($ref->parts);
    for ($p=0; $p<$c; $p++) {
      if (empty($ofs)) $of = ''.($p+1); else $of = $ofs.'.'.($p+1);
      parseparts($ref->parts[$p], $of);
    };
  };
  // Get info on each parts

  parseparts($msg_struct);

 [2001-04-04 06:26 UTC] svein at edbc dot no
The parameter passed to the function i prev. post is the stricture returned from imag_fetchstructure...

- Svein
 [2001-04-04 08:28 UTC] sniper@php.net
I asked for 'self-containing' script. ie. one that doesn't
need anything outside but works as is. This example script
you added is useless and can not be used to reproduce anything. Please create a SHORT (max 5 lines) script that doesn't work.

--Jani

 [2001-04-04 08:34 UTC] svein at edbc dot no
I'm sorry, but all other script i make work ok. Its just this one that cause the problem.

I dont know how to make another script for you as i cant reproduce the error in any 5 line of code.

- Svein
 [2001-06-17 05:06 UTC] jmoore@php.net
I have spent twenty mins trying to recreate this, before you start your for loop please check the value of $c (echo it out etc) and make sure its what you expected. I very much doubt this is a bug, if the value of $c is indeed 0 then please reopen this report.

- James
 [2001-06-17 13:12 UTC] svein at edbc dot no
I have solved the problem in another way. BUT; The value of $c *IS* 0 when entering for loop and the stange behavior of the for-loop explained above happens. I think it maybe is other part of the code BEFORE this function who make som "crash" in the system so this happen. I've never seen this before neither after so i dont know what more to say :)
But for sure it was a strange behavior of PHP in this code. We are two persons coding this system and we both spent a lot of time trying to find out what happens (with echo and other debug code) but we could'nt find out.
We had to drop the for-loop because it didnt work as it should.
 [2001-07-09 07:51 UTC] jeroen@php.net
Please cut the background-color lines etc, and any line
until it won't reproduce the bug. Then submit it again. And
put a var_dump of all your imported globals and on the
passed parameters on the beginning of the function, and
submit it when you still think it's a bug.

The script is too complicated to tell wether this is a bug,
note that you pass $ref BY VALUE, and not by reference.

Closed for now.
 [2001-07-09 07:54 UTC] jeroen@php.net
Please cut the background-color lines etc, and any line
until it won't reproduce the bug. Then submit it again. And
put a var_dump of all your imported globals and on the
passed parameters on the beginning of the function, and
submit it when you still think it's a bug.

The script is too complicated to tell wether this is a bug,
note that you pass $ref BY VALUE, and not by reference.

Closed for now.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 19:01:33 2024 UTC