15 1 0 4000 1 https://codeblock.co.za 300 true 0
theme-sticky-logo-alt

in_array()

0 Comments

Programming Language

PHP [Versions: 4, 5, 7, 8]

Syntax

Typical Usage

Checks an array to determine if a value exists.

Accepted Parameters

  • $needle (mixed) (Required): The value to look for.
  • $haystack (array) (Required): The array to search.
  • $strict (boolean) (Optional) (Default = false): If set to true, data types will also be compared. ie, the integer 1 will be seen as different from the string “1” .

Return Value(s)

Returns true or false depending on whether or not the requested value exists.

Notes

Important:

If $needle is a string, the comparison is done in a case-sensitive manner.

Examples

Example 1

<?php
$pets_array = ['dogs', 'cats', 'birds', 'fish'];

if(in_array('cats', $pets_array)){
    echo 'Yes, she has cats.';
}
else {
    echo 'No, she doesn't have a single cat.';
}

The above will output “Yes, she has cats.” because ‘cats’ is in the array.

Was This Helpful?

Previous Post
empty()

0 Comments

Leave a Reply