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
    Regular Coder
    Join Date
    Feb 2007
    Posts
    217
    Thanks
    25
    Thanked 1 Time in 1 Post

    rewriting array (and code)

    I'm using the code below to check if an value is in my array.. It works, however I'm sure it can be written better.. like all the array value's in one line.. I don't know how to do this though..any help would be appreciated.

    <?php
    $arraynaam['0'] = "17";
    $arraynaam['1'] = "1";

    if ( in_array ("17", $arraynaam) ) {

    echo "17 is in the array !";

    }
    else {

    echo "17 ain't in the array";

    }

    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Not sure if you're asking about the assignment or determination:
    PHP Code:
    $arraynaam = array('17''1');

    print 
    in_array('17'$arraynaam) ? "17 is in the array !" "17 ain't in the array"
    This is called the ternary operator.
    On a side note, though PHP uses loose datatyping and 17 == '17', 17 !== '17'. The quotations indicate that the values are string, not numbers, so it will perform the equality check based on the string radix values instead of the numerical data.
    Last edited by Fou-Lu; 03-10-2009 at 10:33 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    docock (03-10-2009)


  •  

    Posting Permissions

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