Definition and Usage
- PHP Version
- 4+
The is_string() function checks if a given value is of the string type.
It returns true if the value is a string, and false otherwise.
The string type represents textual data. A string must be enclosed in either single quotes ('') or double quotes ("").
Basic Example
/* Strings */
var_dump(is_string('Welcome')); // bool(true)
var_dump(is_string('10')); // bool(true)
var_dump(is_string('0')); // bool(true)
var_dump(is_string('')); // bool(true) -> An empty string is still a string
var_dump(is_string(' ')); // bool(true) -> A whitespace string is also a string
var_dump(is_string("Wrapped in double quotes")); // bool(true) -> A string enclosed in double quotes is also a string
/* Integers */
var_dump(is_string(10)); // bool(false) -> An integer is not a string
/* Floating-point numbers (float) */
var_dump(is_string(10.25)); // bool(false) -> A floating-point number is not a string
/* Booleans */
var_dump(is_string(true)); // bool(false) -> A boolean value is not a string
var_dump(is_string(false)); // bool(false) -> A boolean value is not a string
/* Null */
var_dump(is_string(null)); // bool(false) -> null is not a string
/* Results of numeric operations */
var_dump(is_string(1 + 1)); // bool(false) -> The result of a numeric operation is not a string
var_dump(is_string(10 / 2)); // bool(false) -> The result of a numeric operation is not a string
/* String concatenation */
var_dump(is_string('1' . '1')); // bool(true) -> The result of string concatenation is a string
/* Concatenation of a string and a number */
var_dump(is_string('1' . 1)); // bool(true) -> The result is a string
/* Type Casting */
var_dump(is_string((string) 1)); // bool(true) -> Casting an integer to a string
var_dump(is_string((string) 1.9)); // bool(true) -> Casting a floating-point number to a string
var_dump(is_string((string) true)); // bool(true) -> Casting a boolean to a string
Note:
The gettype() function returns the data type of the given value as a string.
Syntax
is_string(mixed $value): bool
Parameters
$value |
Required. The value to be checked for the string type. |
|---|
Return Values
If the value passed as a parameter is of the string type, the function returns true; otherwise, it returns false.
Things to Keep in Mind
There are several important points to be aware of when using the is_string() function.
Checking the String Representation of an Object
The is_string() function always returns false for objects, even if the object implements the __toString() method.
class MyObject {
public function __toString() {
return "This is an object";
}
}
$obj = new MyObject();
var_dump(is_string($obj)); // bool(false)
var_dump(is_object($obj)); // bool(true)
Code Explanation
The is_object() function checks whether a given value is of the object type.
If the value passed as a parameter is an object, the function returns true; otherwise, it returns false.
This behavior occurs because the is_string() function does not call the __toString() method. The purpose of the is_string() function is to check whether the input value itself is of the string type.
For this reason, even if the __toString() method returns a string, the is_string() function still returns false. Although such values may be output like strings, their actual data type is an object.
Distinguishing Between an Empty String and null
An empty string ('') is considered a value of the string type, but null is not. When working with data, it is important to clearly distinguish between an empty string and null.
o check whether a value is null, use the is_null() function.
Additional Explanation
The is_null() function checks whether a given value is of the NULL type.
If the value passed as a parameter is of the NULL type, the function returns true; otherwise, it returns false.
$empty_string = '';
$null_value = null;
var_dump(is_string($empty_string)); // true
var_dump(is_string($null_value)); // false
var_dump(is_null($empty_string)); // false
var_dump(is_null($null_value)); // true
is_string() and gettype() Function Comparison
In PHP, two commonly used functions for checking whether a variable is of the string type are is_string() and gettype().
There are several differences between these two functions.
In the following sections, we will compare them and examine their respective characteristics.
Additional Explanation
The gettype() function returns the data type of the given value as a string.
$str1 = 'Hello, world!';
$str2 = 'Welcome! Nice to meet you!';
$str3 = '';
echo gettype($str1); // 'string'
echo gettype($str2); // 'string'
echo gettype($str3); // 'string'
When performing a simple type check, the is_string() function is more intuitive and efficient than the gettype() function.
he is_string() function focuses specifically on determining whether the input value is a string, which helps keep the code simple and improves execution efficiency.
For this reason, it is generally the most appropriate choice when checking whether a value is a string.
In contrast, the gettype() function focuses on returning the data type of the input value as a string.
As a result, it performs more work than the is_string() function. However, when it is necessary to identify the exact data type of a value or to handle values differently depending on their type, the gettype() function may be more suitable.
In such cases, the is_string() function alone does not provide sufficient information.
Therefore, it is important to choose the appropriate function based on the given situation.
- When you simply need to check whether a value is a string, using the
is_string()function is recommended. - When you need to determine the exact data type of a value, using the
gettype()function is more appropriate.
References
See also
- PHP gettype() Function – Get the Data Type of a Variable as a String
- PHP is_bool() Function – Checking the Boolean Type of a Variable
- PHP is_int() Function – Checking the Integer Type of a Variable
- PHP is_float() Function – Checking the Float Type of a Variable
- PHP is_numeric() Function – Checking Whether a Variable’s Value Is a Number or a Numeric String
- PHP isset() and empty() Functions – Concepts, Usage, and Key Differences
- PHP Superglobals – Simple and Essential Usage Guide