Related Articles

Related Articles

ArrayObject offsetExists() Function in PHP
  • Last Updated : 26 Mar, 2019

The offsetExists() function of the ArrayObject class in PHP is used to whether a given offset or index is present in the ArrayObject or not. If it is present then the function returns a boolean True value otherwise it returns False.

Syntax:

bool offsetExists($index) 

Parameters: This function accepts a single parameter $index which is the index which is to be checked if it is present in the ArrayObject.

Return Value: This function returns a boolean value True or False based on whether the index is present in the ArrayObject or not.

Below programs illustrate the above function:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate the
// offsetExists() function
  
$arr = array("geeks100", "geeks99", "geeks1", "geeks02");
  
// Create array object
$arrObject = new ArrayObject($arr);
  
// Print the ArrayObject
print_r($arrObject);
  
// Check if the Key 1 is present 
if($arrObject->offsetExists(1))
    echo "\nThe key 1 is present!";
else
    echo "\nThe key 1 is not present!";
  
// Check if the Key 20 is present 
if($arrObject->offsetExists(20))
    echo "\nThe key 20 is present!";
else
    echo "\nThe key 20 is not present!";    
      
?>

chevron_right


Output:

ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [0] => geeks100
            [1] => geeks99
            [2] => geeks1
            [3] => geeks02
        )

)

The key 1 is present!
The key 20 is not present!

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate the
// offsetExists() function
  
$arr = array("Welcome"=>"1", "to" => "2", "GfG" => "3");
  
// Create array object
$arrObject = new ArrayObject($arr);
  
// Print the ArrayObject
print_r($arrObject);
  
// Check if the Key "Welcome" is present 
if($arrObject->offsetExists("Welcome"))
    echo "\nThe key Welcome is present!";
else
    echo "\nThe key Welcome is not present!";
  
// Check if the Key GfG is present 
if($arrObject->offsetExists("GfG"))
    echo "\nThe key GfG is present!";
else
    echo "\nThe key GfG is not present!";    
      
?>

chevron_right


Output:

ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [Welcome] => 1
            [to] => 2
            [GfG] => 3
        )

)

The key Welcome is present!
The key GfG is present!

Reference: http://php.net/manual/en/arrayobject.offsetexists.php




My Personal Notes arrow_drop_up
Recommended Articles
Page :