PHP - Function ctype_space()



The PHP Character type checkingctype_space()is used to determine whether all the characters in the provided string create white space. The white space characters include spaces, tabs (\t), newlines (\n), carriage returns (\r), and other similar characters.

This function returns a boolean value true if the provided string contains the characters that create white spaces; otherwise, it returns false. If the provided string is empty (""), this function will always return "false".

Syntax

Following is the syntax of the PHP Character type checking ctype_space() function −

ctype_space (mixed $text): bool

Parameters

This function accepts the following parameters −

  • text (required) − A string needs to be tested (checked).

Return Value

This function returns "true", if every character in the text creates some sort of white space; "false" otherwise.

Example 1

The following is the basic example of the PHP ctype_space() function −

<?php
   $string = "\n\r\t";
   echo "The given string is: $string";
   echo "Does the string '$string' contain characters that create white space? ";
   #using ctype_space() function
   var_dump(ctype_space($string));
?>

Output

The above program produces the following output −

The given string is:
        Does the string '
        ' contain characters that create white space? bool(true)

Example 2

If the provided string does not consist of the characters that create white space, the PHP ctype_space() function will return false

<?php
   $string = "Tutorialspoint@#5";
   echo "The given string is: $string";
   echo "\nDoes the string '$string' contain characters that create white space? ";
   #using ctype_space() function
   var_dump(ctype_space($string));
?>

Output

After executing the above program, the following output will be displayed −

The given string is: Tutorialspoint@#5
Does the string 'Tutorialspoint@#5' contain characters that create white space? bool(false)

Example 3

If the given string (text) is empty (""), this function will always return 'false' −

<?php
   $string = "";
   echo "The given string is: $string";
   echo "\nDoes the string '$string' contain characters that create white space? ";
   #using ctype_space() function
   var_dump(ctype_space($string));
?>

Output

Once the above program is executed, it generates the following output −

The given string is:
Does the string '' contain characters that create white space? bool(false)

If the input string is empty (" ") but contains white spaces, this function will return 'true' because it checks for characters that create white space in the output.

php_function_reference.htm
Advertisements