count() function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
husniteja
Forum Newbie
Posts: 10
Joined: Fri Feb 25, 2005 8:33 pm
Location: south korea

count() function

Post 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++)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
husniteja
Forum Newbie
Posts: 10
Joined: Fri Feb 25, 2005 8:33 pm
Location: south korea

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
husniteja
Forum Newbie
Posts: 10
Joined: Fri Feb 25, 2005 8:33 pm
Location: south korea

Post 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 ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
husniteja
Forum Newbie
Posts: 10
Joined: Fri Feb 25, 2005 8:33 pm
Location: south korea

Post by husniteja »

ohh oke feyd, i got it, for second line have 3 element so that's why it will print 12

thanks :)
Post Reply