PHP | ctype_space() Function

A ctype_space() function in PHP is used to check whether each and every character of a string is whitespace character or not. It returns True if the all characters are white space, else returns False.

Syntax :

ctype_space(string text)

Parameter Used:



  • text :- It is a mandatory parameter which specifies the string.

Return Value:
Returns TRUE if every character in text contains white space, return false otherwise. the blank character this also includes tab, carriage, vertical tab, line feed, and form feed characters.

Examples:

Input  : \r   
Output : Yes
Explanation: \r is create white space


Input  : abc\r
Output : No   
Explanation: characters "abc" are not white space

Below programs illustrate the ctype_space() function.
Program 1: taking a single string

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check given string is 
// whitespace character or not
  
$string = "/n/t/r";
  
if (ctype_space($string)) 
   echo "Yes \n";
else 
   echo "No \n";   
?>

chevron_right


Output:

No

Program: 2 Taking an array of string and checking for whitespace using ctype_space() function

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check given string is 
// whitespace  character or not
  
$strings = array('\n\y\t\x\u\o', "\ngfg\n", "\t");
  
// Checking above given strings 
//by used of ctype_space()function .
foreach ($strings as $testcase) {
      
    if (ctype_space($testcase)) {
        echo "Yes \n";
    } else {
        echo "No \n";
    }
}
?>

chevron_right


Output:

No 
No 
Yes

Program: 3
Takes an example ctype_space() function how to work string in both single quotes ‘ ‘ and double quotes ” ” symbol.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check given string is 
// whitespace  character or not
  
$strings = array('\n', "\n", '\t', "\t");
  
// Checking above given strings 
// by used of ctype_space()function .
  
foreach ($strings as $testcase) {
      
    if (ctype_space($testcase)) {
        echo "Yes \n";
    } else {
        echo "No \n";
    }
}
?>

chevron_right


Output:

No 
Yes 
No 
Yes

References :http://php.net/manual/en/function.ctype-space.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.