PHP Conference Japan 2024

SplFixedArray::offsetExists

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

SplFixedArray::offsetExists傳回請求的索引是否存在

說明

public SplFixedArray::offsetExists(int $index): bool

檢查請求的索引 index 是否存在。

參數

index

正在檢查的索引。

傳回值

如果請求的 index 存在,則傳回 true,否則傳回 false

新增筆記

使用者貢獻的筆記 1 則筆記

depoemarc at swap dot fn dot ln dot googlemail dot com
9 年前
需要注意的是,offsetExists 的行為類似於 "offsetIsSet" 而不是 "offsetIsValid"

<?php
$arr
= new SplFixedArray(3);
var_dump($arr->offsetExists(1)); // false

$arr[1] = 42; // $arr->offsetSet(1, 42);
var_dump($arr->offsetExists(1)); // true

$arr[1] = null; // $arr->offsetSet(1, null);
var_dump($arr->offsetExists(1)); // true

unset($arr[1]); // $arr->offsetUnset(1);
var_dump($arr->offsetExists(1)); // false

var_dump($arr);
/*
object(SplFixedArray)[1]
null
null
null
*/
?>
To Top