Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
character
whitespace
communitycreator

What is the ctype_space method in PHP?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The ctype_space method can be used to check if all the characters of the text are only whitespace characters.

Syntax

ctype_space(mixed $text): bool

This method returns a Boolean value.

Whitespace characters include horizontal tab, vertical tab, line feed, carriage return, and form feed characters.

Code

<?php
$str = " ";
printIsSpace($str);
$str = "\t\n\r";
printIsSpace($str);
$str = "A";
printIsSpace($str);
$str = "a b";
printIsSpace($str);
function printIsSpace($str) {
echo "The string is: ". $str. "\n";
echo "Is Space: ";
var_dump(ctype_space($str));
echo "---------\n";
}
?>

Explanation

In the code above:

  • We use the ctype_space method to check if the string contains only whitespace characters.

  • For the strings " " and \t\n\r, the ctype_space method returns true.

  • But for the strings A and a b, the ctype_space method returns false because the strings contain non-whitespace characters.

RELATED TAGS

php
character
whitespace
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring