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 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts

    In_Array Always True?

    Why won't this work? For some reason, $checking is always coming up true and it's just echoing $search. It works when I put in_array within the if() statement, but is there a way to keep it outside?

    PHP Code:
    <?php
        $text 
    = array("blah""blah2""blah3");
        
    $checking in_array("not-in-array"$text);
        if (
    $checking 1) {
            echo 
    "This shouldn't appear.";
        }
        else {
            echo 
    "There are no results!";
        }
    ?>
    I'm using this for this, even when I change $search to something random, it still comes out to be true:

    PHP Code:
    <?php
        $text 
    = array("blah""blah2""blah3");
        
    $search $text['0'];
        
    $checking in_array($search$text);
        if (
    $checking 1) {
            echo 
    $search;
        }
        else {
            echo 
    "There are no results!";
        }
    ?>
    Last edited by RonnyNishimoto; 07-18-2012 at 08:11 PM.

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    PHP Code:
    if ($checking 1) { 
    This is not checking if $checking is 1. This is assigning 1 to $checking (look just one line above where you actually do the assignment. As the assignment is successful, it returns true. It will always be true.

  • Users who have thanked Keleth for this post:

    RonnyNishimoto (07-18-2012)

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts
    Wait how can I get this to work?

    PHP Code:
    <?php
    $text 
    = array("blah""blah2""blah3");
    $search $text['1'];
    global 
    $checking;
    $checking in_array($search$text);
    if (
    $checking 1) {
        echo 
    $search;
    }
    else {
        echo 
    "There are no results!";
    }
    ?>
    Last edited by RonnyNishimoto; 07-18-2012 at 02:59 AM.

  • #4
    New Coder
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    34
    Thanks
    11
    Thanked 4 Times in 4 Posts
    = Is an assigning operator.
    == Is a comparing operator.

    You want to compare $checking to 1 and therefore you want to use ==. You could actually just get away with using if ($checking) { as well but that's coder preference.

  • Users who have thanked Cloud Ghost for this post:

    RonnyNishimoto (07-18-2012)

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by Cloud Ghost View Post
    = Is an assigning operator.
    == Is a comparing operator.
    +1

    PHP does not use the = operator like some other languages.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #6
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts


  •  

    Posting Permissions

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