SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: why $count -1

  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    why $count -1

    hi all

    using this code to multi delete items.

    PHP Code:
    $selcheckbox=array();   
    $selcheckbox=$_REQUEST['item'];   
    $count=count($selcheckbox);    
    for(
    $i=0;$i<$count-1;$i++) {      
    $del_id $selcheckbox[$i];          
    $qry2 "delete FROM table ......";

    it works fine but i want to know why it is using $count-1
    PHP Code:
    $i<$count-
    vineet

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Simple but fundamental. Arrays are zero based by default.

    So your count might be 3 items, but they are accessed with the keys 0,1,2

    Hence to access the first item (1) it is $count -1 (0).

  3. #3
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hi cups

    thanks for the explanation

    vineet


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •