PHP - get_resource_type() 函式



定義和用法

get_resource_type() 函式返回資源的型別。

語法

string get_resource_type ( resource $handle )

引數

序號 引數和描述
1

handle

已評估的資源控制代碼。

返回值

  • 如果給定的handle 是一個資源,則此函式將返回一個表示其型別的字串。

  • 如果此函式未識別該型別,則返回值將為字串Unknown

  • 如果handle 不是資源,則此函式將返回null 並生成錯誤。

依賴項

PHP 4.0.2 及更高版本。

示例

以下示例演示了 get_resource_type() 函式與有效資源一起使用的用法 -

<?php
   $resource = fopen("test.txt", "w");
   echo get_resource_type($resource) . "\n";
?>

輸出

這將產生以下結果(它返回一個表示其型別的字串) -

stream

示例

以下示例演示了 get_resource_type() 函式與已釋放資源一起使用的用法 -

<?php
   $resource = fopen("test.txt", "w");
   fclose($resource);
   echo get_resource_type($resource) . "\n";
?>

輸出

這將產生以下結果(它返回字串Unknown) -

Unknown

示例

以下示例演示了當handle 為 null 時 get_resource_type() 函式的用法 -

<?php
   $resource = null;
   echo get_resource_type($resource) . "\n";
?>

輸出

這將在日誌中生成以下錯誤 -

PHP Warning:   get_resource_type() expects parameter 1 to be resource, null given
php_variable_handling_functions.htm
廣告