Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8987053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:41:54+00:00 2026-06-15T21:41:54+00:00

I was trying to understand the in_array behavior at the next scenario: $arr =

  • 0

I was trying to understand the in_array behavior at the next scenario:

$arr = array(2 => 'Bye', 52, 77, 3 => 'Hey');
var_dump(in_array(0, $arr));

The returned value of the in_array() is boolean true. As you can see there is no value equal to 0, so if can some one please help me understand why does the function return true?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:41:56+00:00Added an answer on June 15, 2026 at 9:41 pm

    This is a known issue, per the comments in the documentation. Consider the following examples:

    in_array(0, array(42));      // FALSE
    in_array(0, array('42'));    // FALSE
    in_array(0, array('Foo'));   // TRUE
    

    To avoid this, provide the third paramter, true, placing the comparison in strict mode which will not only compare values, but types as well:

    var_dump(in_array(0, $arr, true));
    

    Other work-arounds exist that don’t necessitate every check being placed in strict-mode:

    in_array($value, $my_array, empty($value) && $value !== '0');
    

    But Why?

    The reason behind all of this is likely string-to-number conversions. If we attempt to get a number from “Bye”, we are given 0, which is the value we’re asking to look-up.

    echo intval("Bye"); // 0
    

    To confirm this, we can use array_search to find the key that is associated with the matching value:

    $arr = array(2 => 'Bye', 52, 77, 3 => 'Hey');
    echo array_search(0, $arr);
    

    In this, the returned key is 2, meaning 0 is being found in the conversion of Bye to an integer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand isolation/locks in SQL Server. I have following scenario in
I'm currently trying to understand how to implement a 2-dimensional array of struct in
I am trying to understand Double-Array Trie implementation from http://linux.thai.net/~thep/datrie/datrie.html But I do not
I am trying to understand the boost array . The code can be read
I am trying to understand the differences between these array definitions: abc=[ 0 0
I am trying to understand how to create array lists in Java, for some
I'm trying to understand the array setup in java. Why must you initialize space
I am trying to understand some slightly odd behavior I am seeing in a
I'm new to JavaScript and trying to understand the behavior of this code, which
Just trying to understand how to address a single character in an array of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.