is_string

is_string(mixed $value): bool

Finds whether a variable is a string.

C C23

C23
#include <stdio.h>

#define is_string(x) _Generic((x), char*: 1, const char*: 1, default: 0)

int main(void) {
    const char *s = "abc";
    int n = 42;
    printf("%d %d\n", is_string(s), is_string(n));
    return 0;
}
Output
1 0

C++ C++26

C++26
#include <iostream>
#include <string>
#include <type_traits>
int main() {
    std::string s = "abc";
    int n = 42;
    std::cout << std::boolalpha
              << std::is_same_v<decltype(s), std::string> << " "
              << std::is_same_v<decltype(n), std::string> << "\n";
    return 0;
}
Output
true false

C# v11.0.100

v11.0.100
object[] xs = { "abc", 42 };
Console.WriteLine(xs[0] is string);
Console.WriteLine(xs[1] is string);
Output
True
False

Go v1.27.4

v1.27.4
package main

import "fmt"

func isString(v any) bool {
    _, ok := v.(string)
    return ok
}

func main() {
    fmt.Println(isString("abc"), isString(42))
}
Output
true false

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        Object[] xs = { "abc", 42 };
        System.out.println(xs[0] instanceof String);
        System.out.println(xs[1] instanceof String);
    }
}
Output
true
false

JavaScript v26.13.1

v26.13.1
console.log([typeof "abc" === "string", typeof 42 === "string"]);
Output
[ true, false ]

PHP v8.6.0

v8.6.0
echo json_encode([is_string("abc"), is_string(42)]);
Output
[true,false]

Python v3.15.2

v3.15.2
print([isinstance(x, str) for x in ["abc", 42]])
Output
[True, False]

Rust v1.99.1

v1.99.1
use std::any::Any;

fn main() {
    let xs: Vec<Box<dyn Any>> = vec![Box::new("abc".to_string()), Box::new(42i32)];
    for x in &xs {
        println!("{}", x.is::<String>());
    }
}
Output
true
false

TypeScript v26.13.1

v26.13.1
const xs: (string | number)[] = ["abc", 42];
console.log(xs.map((x) => typeof x === "string"));
Output
[ true, false ]

Language differences

C: _Generic dispatches on types at compile time.

C++: std::is_same_v dispatches on types at compile time.

C#: is on object holders.

Go: the type assertion v.(string).

Java: statically typed languages know the variable type at compile time — instanceof on object holders.

JavaScript: typeof x === "string".

PHP: is_string strictly checks the string scalar type.

Python: uses isinstance(x, str).

Rust: boxes the value as Box<dyn Any> and checks with Any::is downcasting.

TypeScript: same as JS — typeof x === "string".