PHP Ds Vector::isEmpty() 函式



PHP 的 Ds\Vector::isEmpty() 函式用於確定當前向量是否為空。如果向量為空 ([]),則此函式返回布林值“true”,否則返回“false”。

Ds\Vector 提供了另一個名為 count() 的函式,該函式返回向量的長度。如果您在“空”向量上呼叫此函式,它將返回 零 (0)

語法

以下是 PHP Ds\Vector::isEmpty() 函式的語法:

public Ds\Vector::isEmpty(): bool

引數

此函式不接受任何引數。

返回值

如果向量為空,則此函式返回“true”,否則返回“false”。

示例 1

以下程式演示了 PHP Ds\Vector::isEmpty() 函式的用法:

<?php 
   $vector = new \Ds\Vector([1, 2, 3, 4, 5]);
   echo "The vector elements are: \n";
   print_r($vector);
   echo "Is the vector is empty? ";
   var_dump($vector->isEmpty());
?>

輸出

執行上述程式後,將顯示以下輸出:

The vector elements are:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
Is the vector is empty? bool(false)

示例 2

如果向量 為空,則此函式將返回“true”。

以下是 PHP Ds\Vector::isEmpty() 函式的另一個示例。我們使用此函式來檢查此向量 ([]) 是否為空:

<?php 
   $vector = new \Ds\Vector([]);
   echo "The vector elements are: \n";
   print_r($vector);
   echo "Is the vector is empty? ";
   var_dump($vector->isEmpty());
   echo "The number of elements in vector: ";
   print_r($vector->count());
?>

輸出

上述程式生成以下輸出:

The vector elements are:
Ds\Vector Object
(
)
Is the vector is empty? bool(true)
The number of elements in vector: 0

示例 3

在條件語句中使用 isEmpty() 函式的結果來確定向量是否為空:

<?php 
   $vector = new \Ds\Vector(['a', 'e', 'i', 'o', 'u']);
   echo "The vector elements are: \n";
   print_r($vector);
   echo "Is the vector is empty? ";
   $res = $vector->isEmpty();
   if($res){
	   echo "Empty";
   }
   else{
	   echo "Not empty";
   }
?>

輸出

執行上述程式後,將顯示以下輸出:

The vector elements are:
Ds\Vector Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
Is the vector is empty? Not empty
php_function_reference.htm
廣告