is_scalar / is_object / is_resource
is_scalar(mixed $value): bool — is_object(mixed $value): bool — is_resource(mixed $value): bool
Scalar/object/resource triple check rounding out PHP's type system: scalar = bool/int/float/string; resource is unique to PHP (an external handle), matched elsewhere by Closeable, FILE*, OwnedFd and friends.
C C23
#include <stdio.h>
int main(void) {
/* PHP's resource is the moral descendant of a FILE*: a handle to
something living outside the value system */
FILE *f = tmpfile(); /* always succeeds: no external file needed */
if (f != NULL) {
printf("open\n");
fclose(f);
printf("closed\n");
}
return 0;
}
open closed
C++ C++26
#include <cstdio>
#include <iostream>
int main() {
std::FILE *f = std::tmpfile(); // a raw handle: the ancestor of PHP's resource
std::cout << (f != nullptr) << "\n";
std::fclose(f);
// RAII is the modern answer: fstream closes itself at scope end,
// so "is the resource still valid" never needs checking
{
std::FILE *g = std::tmpfile();
std::cout << (g != nullptr) << "\n";
std::fclose(g);
}
std::cout << "closed\n";
return 0;
}
1 1 closed
C# v11.0.100
// PHP resource ≈ IDisposable: an external handle with explicit lifecycle.
// The using statement closes it at scope end - no is_resource check exists.
using var handle = new System.IO.MemoryStream();
Console.WriteLine(handle.CanRead); // true: the handle is live
// after the using scope the handle is disposed automatically
True
Go v1.27.4
package main
import (
"fmt"
"os"
)
func main() {
// Go has no resource type: an open file is just an *os.File struct
// value - "is it open?" is an ordinary nil/method question
f, err := os.CreateTemp("", "demo")
if err != nil {
return
}
fmt.Println(f != nil)
name := f.Name()
f.Close()
os.Remove(name)
}
true
Java v27.0.1
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
// PHP resource ≈ Closeable: a handle to something outside the heap.
// try-with-resources closes it - Java's answer to fclose
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
System.out.println(out.size()); // 0: the handle is live
}
}
}
0
JavaScript v26.13.1
// JavaScript has no out-of-heap resource type: even "external" things
// (files, sockets in Node) are wrapped in ordinary objects - and the
// low-level fd numbers are plain numbers, hidden behind the wrapper
const fs = require("node:fs");
const fd = fs.openSync("/dev/null", "r");
console.log(typeof fd); // number: under the hood it is a Unix fd
fs.closeSync(fd);
number
PHP v8.6.0
// scalar = bool, int, float, string: the "plain value" family
var_dump(is_scalar(1), is_scalar(1.5), is_scalar(true), is_scalar('x'));
var_dump(is_scalar([1])); // arrays are NOT scalar
var_dump(is_scalar(null)); // null is NOT scalar either
var_dump(is_object(new stdClass()));
var_dump(is_object('string'));
// resource: PHP's own type for external handles - no other mainstream
// language has a dedicated type like this
$r = fopen('php://memory', 'r+');
var_dump(is_resource($r));
fclose($r);
var_dump(is_resource($r)); // closed: the resource is gone
bool(true) bool(true) bool(true) bool(true) bool(false) bool(false) bool(true) bool(false) bool(true) bool(false)
Python v3.15.2
f = open("/dev/null")
print(type(f).__name__) # TextIOWrapper: just an ordinary object
f.close()
print(f.closed) # True: the state lives on the object
# Python has no resource type either: external handles are ordinary
# objects with close/__del__ - checked with isinstance, not a type test
TextIOWrapper True
Rust v1.99.1
fn main() {
// Rust gives every external handle its own precise type: File,
// TcpStream, OwnedFd - there is no generic "resource" bag to test
let f = std::fs::File::create("/dev/null").unwrap();
println!("{}", f.metadata().is_ok());
// Drop closes the file automatically (RAII): no explicit close,
// and a closed file cannot even be named - moved/dropped values
// are rejected at compile time
}
true
TypeScript v26.13.1
// TypeScript has no out-of-heap resource type either: external handles
// are wrapped in ordinary objects; fd numbers are plain numbers
import * as fs from "node:fs";
const fd = fs.openSync("/dev/null", "r");
console.log(typeof fd); // number
fs.closeSync(fd);
number
Language differences
C: FILE* is resource's ancestor - open files, sockets and pipes all return opaque pointers/handles; a closed FILE* is a dangling pointer, and C uses NULL checks rather than an is_resource-style type test.
C++: raw handles carry over from C; the modern answer is RAII (fstream/unique_lock) - the resource lifetime is bound to an object, and "the resource after close" cannot even be touched at the type level, so no check is needed.
C#: IDisposable is resource's counterpart - external handles carry an explicit lifecycle, managed by using/await using; there is no is_resource-style runtime check, and consistency is enforced by the compiler and analyzers.
Go: no resource type - an open file is just an *os.File struct value, and "is it open" is an ordinary nil/method question; handles travel as plain values and the GC does not close them (defer f.Close() is the idiom).
Java: the AutoCloseable/Closeable interfaces plus try-with-resources are resource's counterpart; calling a closed stream throws IOException - "the resource is dead" is expressed through the exception system, not a type check.
JavaScript: no out-of-heap resource type - Node wraps files/sockets as ordinary objects (fs.ReadStream etc.), and the underlying fd is just a number; resource validity is expressed via events (error/close) and object properties.
PHP: resource IS a PHP-only type - file handles, database connections and image canvases are all resources; scalars round out to bool/int/float/string (is_scalar returns false for both arrays and null). Historical legacy: resources wrap OS fds/handles and are released when the script ends; after closing, is_resource returns false but the variable remains. Modern PHP is gradually shrinking resource (PDO became objects in 8.4, file_info and others too).
Python: no resource type - external handles are ordinary objects with close/__del__ (TextIOWrapper etc.); the closed property and isinstance suffice; the with statement carries fclose's semantics.
Rust: one precise type per external handle (File/TcpStream/OwnedFd) - "is it a resource" is not a question, the type is the answer; RAII Drop guarantees release and a dangling handle is inexpressible at compile time.
TypeScript: same as JavaScript.