PHP in_array() Array Function Example

tutspack laravel
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Hello Programmers,

In this tutorial, I will discuss with you a PHP built in function which is in_array().

The PHP in_array() function is a built-in function. in_array() function checks if a value exists or not in a array. It returns TRUE if the given items are found in the given array, and returns FALSE otherwise.

PHP in_array() Syntax

The PHP in_array() function syntax is in_array(search, array, type)

search :Required. Specifies what to search for

array : Required. it specifies the array in which we want to search.

type : Optional. If this parameter is set to TRUE, the in_array() function searches for the search-string and specific type in the array.

Example : 

  $items = array("Mobile", "TV", "Camera", "iPhone");

  return (in_array("iPhone", $items))
      
         ? "Yes" : "Not";
         

Output : Yes

Read More : 10 PHP interview Questions

More Example of php array (in_array) function :

$image = $_FILES['image']['name'];
        $permit = array('png','jpg','jpeg');
        $ext = explode('.',$image);
        $ext = strtolower(end($ext));
        if(in_array($ext,$permit) === false){
            echo "EXT_NOT_MATCH";
            exit();
        }else{
       //go for next request
}

Reference:
http://php.net/manual/en/function.in-array.php

Read More : laravel api CRUD with sanctum

Hope it can help you.


Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
  • 1
    Share

About Anisur Rahman Shahin

Hello. My name is Shahin. I'm a tech enthusiast guy. Personally, I’m Optimistic and always in hurry kinda person. I'm a freelance web developer. I am working at Zakir Soft as Laravel Developer. My Portfolio website: https://devshahin.com/

View all posts by Anisur Rahman Shahin →

2 Comments on “PHP in_array() Array Function Example”

  1. Hi there, I discovered your website by the use of Google whilst searching for a
    similar subject, your site got here up, it seems great. I’ve bookmarked it
    in my google bookmarks.
    Hello there, just was alert to your blog via Google, and found that it’s really informative.
    I am gonna watch out for brussels. I will be grateful should you continue this
    in future. A lot of people will be benefited from
    your writing. Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *