Character checks7 min readExamples run on PHP 8.5.10

ctype_space()

Learn how PHP 8.4 ctype_space() tests whether every character in a string is whitespace, including its empty-string result, escape-sequence pitfalls and related functions.

Signature

ctype_space(mixed $text): bool
Available since PHP 4.0.47.48.08.18.28.38.48.5All versions →

Purpose

ctype_space() answers one narrow question: does the given string consist entirely of whitespace characters? It returns true only when every character is whitespace and the string contains at least one character. It is useful for detecting blank form fields, skipping empty lines in a parser, or validating that a separator token really is a separator.

The whitespace set covers the ordinary blank (space), tab, vertical tab, line feed, carriage return and form feed. The check works on bytes, one at a time, so it is an ASCII-oriented tool, not a Unicode-aware one.

Signature

Parameters and defaults

$text is the value to test. It has no default and is required. The intended input is a string.

The manual describes a legacy behavior for integers. An integer from -128 to 255 is treated as the ASCII code of a single character, with negative values having 256 added. Any other integer is treated as a string of its decimal digits. As of PHP 8.1.0, passing a non-string argument is deprecated, and the manual says a future version will interpret it as a string instead. Cast explicitly with (string) or call chr() when you mean a character code, so that your code does not depend on this behavior.

Return value and failure cases

The function returns a bool:

  • true if every character is whitespace.
  • false if at least one character is not whitespace.
  • false for an empty string. This surprises many people, because an empty string contains no non-whitespace characters, yet the function always reports false.

The function does not throw for ordinary strings. The only failure-like situation is passing a non-string value, which triggers the deprecation described above.

Example 1: basic checks

The first example tests a few strings, including a mix of all the whitespace characters and an empty string.

Example
<?php
$tests = [
    'spaces' => "   ",
    'mixed whitespace' => "\n\r\t \v\f",
    'letters' => "a b",
    'empty' => "",
];
$result = [];
foreach ($tests as $label => $text) {
    $result[$label] = ctype_space($text);
}
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
{"spaces":true,"mixed whitespace":true,"letters":false,"empty":false}

One non-whitespace character, such as the a in "a b", makes the whole result false, even though the string contains a space.

Example 2: real escapes versus literal backslashes

A frequent mistake is writing whitespace escapes inside single quotes. PHP only expands \n and \t in double-quoted strings. In single quotes you get a backslash followed by a letter, which are ordinary non-whitespace characters.

Example
<?php
$real = "\n\t";
$literal = '\n\t';
echo json_encode([
    'real' => ctype_space($real),
    'literal' => ctype_space($literal),
    'length_real' => strlen($real),
    'length_literal' => strlen($literal),
]), "\n";
Output
{"real":true,"literal":false,"length_real":2,"length_literal":4}

The real string has two bytes, while the literal one has four. That is why the second test is false.

Example 3: a blank-field helper

Because the empty string yields false, a helper for "blank input" usually needs to handle that case explicitly.

Example
<?php
function isBlankField(string $value): bool
{
    return $value === '' || ctype_space($value);
}

$inputs = ["", "  ", "\t\n", " x ", "0"];
$out = [];
foreach ($inputs as $input) {
    $out[] = isBlankField($input);
}
echo json_encode($out), "\n";
Output
[true,true,true,false,false]

Note that "0" is not blank: it is a visible character, so the helper returns false.

Edge cases

  • Empty string: always false. Add a separate === '' check if you want to treat it as blank.
  • Mixed content: a single non-whitespace byte anywhere in the string makes the result false.
  • Multibyte whitespace: characters such as a non-breaking space are encoded as several bytes in UTF-8. The ctype functions inspect bytes using the current character-type rules, and in the default C locale those bytes are not whitespace. For Unicode-aware checks, consider IntlChar::isspace() or a PCRE pattern with the u modifier.
  • Non-string input: deprecated as of PHP 8.1.0. Cast or convert deliberately.

Common mistakes

  1. Expecting true for an empty string.
  2. Using single-quoted strings with \n, \t or \r and expecting real control characters.
  3. Assuming the function handles Unicode whitespace.
  4. Passing numbers and relying on the ASCII-code interpretation, which the manual flags as deprecated for non-strings.
  5. Using ctype_space() to test "has no content" after values may contain other invisible characters. It only recognizes the whitespace set listed above.
  • ctype_cntrl() checks for control characters.
  • ctype_graph() checks for printable characters except space.
  • ctype_punct() checks for printable characters that are neither whitespace nor alphanumeric.
  • IntlChar::isspace() checks whether a code point is a space character.
  • trim() removes surrounding whitespace and is often used instead when you only need to know whether any content remains.

Recap

ctype_space() returns true only for a non-empty string made entirely of whitespace bytes. Remember that the empty string gives false, use double quotes for real escape sequences, and avoid passing non-strings. For Unicode text, reach for a Unicode-aware alternative.

Tested examples

Basic whitespace checks
<?php
$tests = [
    'spaces' => "   ",
    'mixed whitespace' => "\n\r\t \v\f",
    'letters' => "a b",
    'empty' => "",
];
$result = [];
foreach ($tests as $label => $text) {
    $result[$label] = ctype_space($text);
}
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
{"spaces":true,"mixed whitespace":true,"letters":false,"empty":false}
Real escapes versus literal backslashes
<?php
$real = "\n\t";
$literal = '\n\t';
echo json_encode([
    'real' => ctype_space($real),
    'literal' => ctype_space($literal),
    'length_real' => strlen($real),
    'length_literal' => strlen($literal),
]), "\n";
Output
{"real":true,"literal":false,"length_real":2,"length_literal":4}
A blank-field helper
<?php
function isBlankField(string $value): bool
{
    return $value === '' || ctype_space($value);
}

$inputs = ["", "  ", "\t\n", " x ", "0"];
$out = [];
foreach ($inputs as $input) {
    $out[] = isBlankField($input);
}
echo json_encode($out), "\n";
Output
[true,true,true,false,false]