Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts

    Using "null" for first value in array?

    I'm not very comfortable counting from 0, is there any problem with just using:

    PHP Code:
    $var = array(null"Mike"=>22); 
    In terms of future languages and the mindset I should be in, would this be a bad idea? Does anyone else use null in order to start at 1?

    Thank you!
    Last edited by RonnyNishimoto; 07-12-2012 at 02:59 AM.

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,077
    Thanks
    11
    Thanked 98 Times in 96 Posts
    In an associative array like yours above there seems little point since you would reference the values by the key , e.g. $var['mike'] not $var[1];
    + if you iterate through your arrays with foreach( etc I don't see how the first value having an index of 0 is of too much concern.

    Personally I would say just get used to counting from 0, if you have an empty value at the start of an array you may later need to add an extra check for such which is IMO wasted code..e.g.

    PHP Code:
    <?
    foreach($arr as $k=>$v){
       echo 
    "<div id=\"$k\">$v</div>";
    }
    ?>
    now needs to be...
    <?
    foreach($arr as $k=>$v){
        if(!empty(
    $v)){
            echo 
    "<div id=\"$k\">$v</div>";
        }
    }
    ?>
    else you get an empty div ... you may need to do such a check for other reasons but with null values you have to do this everywhere ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • Users who have thanked firepages for this post:

    RonnyNishimoto (07-11-2012)


  •  

    Posting Permissions

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