ctype_space

Checks whether every character in a string is whitespace, such as a space, tab or newline.

Signature
ctype_space(mixed $text): bool
  • Added in PHP 4.0.4

Parameters

ParameterTypeDescription
$textmixedThe string to test. Every byte must be a whitespace character for the result to be true. An int between -128 and 255 is interpreted as an ASCII codepoint, which is why non-string arguments are deprecated as of PHP 8.1.

Return value

booltrue if every character in text creates some sort of white space, false otherwise. An empty string always returns false.

Errors

Never throws. A non-string argument raises an E_DEPRECATED notice as of PHP 8.1; values that are neither string nor int return false.

Changelog

  • PHP 8.1Passing a non-string argument is deprecated. Ints between -128 and 255 are still read as ASCII codepoints, so cast to string or use chr() explicitly.

ctype_space() is true when every character is whitespace. The set is wider than most people assume: space, tab (\t), line feed (\n), carriage return (\r), vertical tab (\v) and form feed (\f).

When to reach for it

  • Skipping blank lines while parsing a file, where a line of spaces and tabs should count as blank.
  • Deciding whether a form field the user filled with spaces is effectively empty.
  • Consuming whitespace one character at a time in a hand-written tokeniser.

What trips people up

The empty string returns false, which is the opposite of what trim($s) === '' gives you. If you want "blank or empty", write $s === '' || ctype_space($s), or just use trim($s) === '' and skip this function entirely.

It also does not know about Unicode whitespace. A non-breaking space (U+00A0) is two bytes in UTF-8 and neither of them is ASCII whitespace, so a string of them returns false even though it looks blank. Those are exactly the characters that arrive from copy-pasted web content, so validate with preg_match('/^\s*$/u', $s) when the source is untrusted.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Skipping blank lines when parsing a config or fixture file
  • Treating a form field containing only spaces as a missing value
  • Consuming whitespace between tokens in a small hand-written parser

Frequently asked questions

Why does ctype_space return false for an empty string?

The documented behaviour is that an empty string always returns false for every ctype function, because there is no character to satisfy the test. Write $s === '' || ctype_space($s) if you want blank-or-empty.

What is the difference between ctype_space and trim?

trim removes whitespace and returns a string, while ctype_space only reports whether the whole string is whitespace. trim($s) === '' is the usual idiom and also treats the empty string as blank.

Does ctype_space detect a non-breaking space?

No. U+00A0 is multi-byte in UTF-8 and is not ASCII whitespace, so ctype_space returns false for it. Use preg_match('/^\s*$/u', $s) for Unicode whitespace.

More examples

Classifying the lines of a text block

Prints content, empty, content, blank, content. Line 1 is empty and line 3 holds only a space and a tab, and the two need separate tests because ctype_space returns false for the empty string.

Example · php
<?php
$lines = explode("\n", "first\n\nsecond\n \t\nthird");

foreach ($lines as $i => $line) {
    $state = $line === ''
        ? 'empty'
        : (ctype_space($line) ? 'blank' : 'content');
    echo $i, ': ', $state, "\n";
}

A correct blank-field guard

Prints true, true, false. The explicit empty-string branch is what makes this equivalent to trim($value) === '', which is the shorter way to write the same check.

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

var_dump(isBlank(''));
var_dump(isBlank("   \t"));
var_dump(isBlank(' x '));

Discussion

  • Be the first to comment on this lesson.