PHP in_array() Array Function Example

tutspack laravel
Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  

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
  •  
  •  
  •  
  •  
  •  
  •  
  •  

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 Bangla Kings Software as Junior Laravel Developer. My Portfolio website: https://devshahin.com/

View all posts by Anisur Rahman Shahin →

Leave a Reply

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