Page 1 of 1

count() function

Posted: Fri Feb 25, 2005 8:58 pm
by husniteja
i made exsternal file and i want to read it string one by one. example :

My exsternal file
check: name,required
check: email,required,email-check
check: comment,required
and in my PHP program i use standard function to open and read the file :

Code: Select all

while (!feof($fd)) {
      $line = fgets($fd, 8192);
            
      if (ereg("^check:", $line)) {

	$check = explode(",", ereg_replace("check:\ *", "", trim($line)));
	$field = $checkї0];
	for ($i = 1; $i < count($check); $i++) &#123;
	  if (ereg("required", $check&#1111;$i])) &#123;
	    $this->required&#1111;] = $field;
	  &#125; else if (ereg("email-check", $check&#1111;$i])) &#123;
	    $this->checkemail&#1111;] = $field;
	  &#125;
	&#125;
      &#125; 
&#125;
In ereg() function, what is meaning ^

Code: Select all

ereg(^check,$line);
and while i try to print count($check) the result is : 2,3,2. what is mean ?
and in my for statement, which value will $i will compare ?

Code: Select all

for ($i = 1; $i < count($check); $i++)

Posted: Fri Feb 25, 2005 9:01 pm
by feyd
^ at the certain places of a regex pattern mark the beginning of a line.

count() returns the number of elements in an array.

the second (middle) expression of a for() is the comparison expression. The first is an initializer. The last is a iteration advance..

Posted: Fri Feb 25, 2005 9:12 pm
by husniteja
hi feyd

i still don't understand about for statement here. because while i tried to print count(), the result is 2,3,2

and if i echo $i inside for statement, the result is 1,1,2,1. i still cannot figure our the idea. may be do you can give me basic sample to understand it.

Code: Select all

$i<count($check)
this code i don't really understand

Posted: Fri Feb 25, 2005 9:16 pm
by feyd
arrays start counting at zero, by default. Since you asked the loop to start at element 1, it has no more elements to look at because when that iteration is finished, $i is incremented by one making it 2. 2 is not less than 2.

Posted: Fri Feb 25, 2005 11:37 pm
by husniteja
hi feyd

No no i don't mean like that, i understand about "for statement". Oke i review my code :

Code: Select all

while (!feof($fd)) &#123; 
      $line = fgets($fd, 8192); 
            
      if (ereg("^check:", $line)) &#123; 

   $check = explode(",", ereg_replace("check:\ *", "", trim($line))); 
   echo count($check);
   $field = $check&#1111;0]; 
   for ($i = 1; $i < count($check); $i++) &#123;
     echo $i; 
     if (ereg("required", $check&#1111;$i])) &#123; 
       $this->required&#1111;] = $field; 
     &#125; else if (ereg("email-check", $check&#1111;$i])) &#123; 
       $this->checkemail&#1111;] = $field; 
     &#125; 
   &#125; 
      &#125; 
&#125;
please look in echo $i (bold in code) and i can look in my browser value
1121, since the number of element array (return from count($check)) is less then 2 how can it be ?

Posted: Fri Feb 25, 2005 11:43 pm
by feyd
remember that you are processing 3 lines... each line has 2, 3, and 2 element arrays each. The for loop starts at element index 1 of each array. That's the last element of the first and last line arrays. It is the second of three elements of the second line array. Since the echo line is called for each iteration of the loop, the output for line 2 is 12.. or 1 and 2. Thus the total output is 1121

Posted: Fri Feb 25, 2005 11:51 pm
by husniteja
ohh oke feyd, i got it, for second line have 3 element so that's why it will print 12

thanks :)