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 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Ā 

php




<?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";Ā Ā 
?>


Output:

No

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

PHP




<?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";
Ā Ā Ā Ā }
}
?>


Output:

No 
No 
Yes

Program: 3 Takes an example ctype_space() function how to work string in both single quotes ā€˜ ā€˜ and double quotes ā€ ā€ symbol.Ā 

PHP




<?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";
Ā Ā Ā Ā }
}
?>


Output:

No 
Yes 
No 
Yes

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

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button