in_array

in_array(mixed $needle, array $haystack, bool $strict = false): bool

Checks whether a value exists in an array.

C C23

C23
#include <stdio.h>
int main(void) {
    int xs[] = {1, 2, 3};
    size_t n = sizeof xs / sizeof xs[0];
    int found = 0; // hand-written linear search
    for (size_t i = 0; i < n; i++) {
        if (xs[i] == 2) { found = 1; break; }
    }
    printf(found ? "true\n" : "false\n");
    // static typing: PHP's loose-match pitfall ("1.5" == 1.5) cannot happen in C
    double dv = 1.5;
    printf("%s\n", dv == 1.5 ? "true" : "false"); // same type: plain == is strict
    return 0;
}
Output
true
true

C++ C++26

C++26
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> xs = {1, 2, 3};
    std::cout << std::boolalpha; // bool prints as true/false
    std::cout << (std::find(xs.begin(), xs.end(), 2) != xs.end()) << "\n";
    // static typing: PHP's loose-match pitfall ("1.5" == 1.5) cannot happen in C++
    double dv = 1.5;
    std::cout << std::boolalpha << (dv == 1.5) << "\n"; // same type: == is strict
    return 0;
}
Output
true
true

C# v11.0.100

v11.0.100
var xs = new List<int> { 1, 2, 3 };
Console.WriteLine(xs.Contains(2));
// static typing: the loose-match pitfall cannot happen; same-type == is strict
Console.WriteLine(1.5 == 1.5);
Output
True
True

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "slices"
)

func main() {
    fmt.Println(slices.Contains([]int{1, 2, 3}, 2))

	// static typing: "1.5" never matches 1.5 — Go has no loose comparison
	fmt.Println(1.5 == 1.5)
}
Output
true
true

Java v27.0.1

v27.0.1
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        System.out.println(Arrays.asList(1, 2, 3).contains(2));
        // static typing: "1.5" never matches 1.5 — Java has no loose comparison
        System.out.println(1.5 == 1.5);
    }
}
Output
true
true

JavaScript v26.13.1

v26.13.1
console.log([1, 2, 3].includes(2))
// JS has the same trap: == coerces types, === is the strict escape hatch
console.log("1.5" == 1.5, "1.5" === 1.5)
Output
true
true false

PHP v8.6.0

v8.6.0
var_export(in_array(2, [1, 2, 3]));
echo "
";
// loose comparison bites: the string needle matches a float element
var_export(in_array("1.5", [1.5]));
echo "
";
// third argument true switches to strict === comparison
var_export(in_array("1.5", [1.5], true));
Output
true
true
false

Python v3.15.2

v3.15.2
print(2 in [1, 2, 3])
# no implicit coercion: "1.5" never equals 1.5 (PHP's loose match cannot happen)
print("1.5" == 1.5, 1.5 == 1.5)
Output
True
False True

Rust v1.99.1

v1.99.1
fn main() {
    println!("{}", [1, 2, 3].contains(&2));

    // static typing: "1.5" never matches 1.5; comparing f64 with f64 is strict
    println!("{}", 1.5_f64 == 1.5_f64);
}
Output
true
true

TypeScript v26.13.1

v26.13.1
const xs: number[] = [1, 2, 3];
console.log(xs.includes(2));
// same trap in JS: == coerces, === stays strict
console.log("1.5" == 1.5, "1.5" === 1.5);
Output
true
true false

Language differences

C: has no built-in containers or higher-order functions; the example uses fixed arrays/buffers and hand-written loops.

C++: searches with std::find, an iterator compared against end().

Go: needs slices.Contains (Go 1.21+).

Java: List.contains.

JavaScript: uses includes.

Python: uses the in operator (booleans print as capitalized True).

Rust: arrays have a built-in .contains.