PHP 变量处理函数

PHP is_resource() 函数检查变量是否是资源。如果变量是资源,该函数返回 true,否则返回 false。

注意:此函数不是严格的类型检查功能。如果 value 是已关闭的资源变量,则返回 false。

语法

is_resource(variable) 

    参数

    variable必填。 指定要计算的变量。

    返回值

    如果变量为资源,则返回 true,否则为 false。

    示例:

    下面的示例显示了 is_resource() 函数的用法。假设当前工作目录中有一个名为 test.txt 的文件。

    <?php
    $file = fopen("test.txt","r");
    
    if (is_resource($file)) {
      echo "File is opened successfully.";
    } else {
      echo "Error in opening the file.";
    }
    ?> 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    上述代码的输出将是:

    File is opened successfully. 

      示例:

      再考虑一个示例,其中该函数与已关闭的资源变量一起使用。

      <?php
      $file = fopen("test.txt","r");
      fclose($file);
      
      if (is_resource($file)) {
        echo "File is opened successfully.";
      } else {
        echo "Error in opening the file.";
      }
      ?> 
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9

      上述代码的输出将是:

      Error in opening the file.