SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    May 2001
    Location
    UK
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Counting the elements in an array / functions prob

    OK. Simple enough task... you use

    PHP Code:
    count($element); 
    Except, it doesn't seem to work! I have the following code..

    PHP Code:
    function mainscript($element){
        
    $numelements count($element);
        echo 
    "We have $numelemets elements<br>";
    if (
    $numelements 49) {
        echo 
    "We have multiple blocks on one line<br>"
    and it produces
    We have elements
    We have multiple blocks on one line
    I don't get it, the if statement should surely only go if there are more than 49 elements (which there wasn't in this example). It seems to be thinking that $element is blank, but further down in the script (still in this function) I use the different parts of the array $element successfully, so really don't get it. Hopefully a simple problem!

    Thanks
    Please take a minute to do The Ecommerce Survey
    You get a free information security report as a thank you!
    [ More info on report | The Ecommerce Survey ]

  2. #2
    SitePoint Evangelist MobileBadBoy's Avatar
    Join Date
    Jun 2002
    Location
    Mobile, Alabama
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I did notice you're missing a n in $numelements:

    echo "We have $numeleme[n]ts elements<br>";
    Shawn Kerr .com

  3. #3
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You spelled the variable name incorrectly.

    It should be.
    PHP Code:
    echo "We have $numelements elements<br>"

  4. #4
    SitePoint Enthusiast Kasai's Avatar
    Join Date
    Nov 2003
    Location
    Germany
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    there's a little typo in your first echo.
    but no other errors. i tested the function myself
    and it worked all fine.

    so i think the problem may be within the array creation rather than this function.

  5. #5
    SitePoint Zealot
    Join Date
    May 2001
    Location
    UK
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doh! Always the simple things that trip you up, had been looking at that for aguess. Thanks guys
    Please take a minute to do The Ecommerce Survey
    You get a free information security report as a thank you!
    [ More info on report | The Ecommerce Survey ]

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A tip: If you set the error level to its highest, you will get a notice about those errors.
    To do it, put this at the top of your script
    PHP Code:
    error_reporting(E_ALL); 
    -Helge


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
  •