Open In App

PHP | ctype_cntrl() Function

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The ctype_cntrl() is an inbuilt function in PHP which is used to check all the characters in string/text are control characters. Control characters are e.g. line feed, tab, escape.
Syntax: 
 

bool ctype_cntrl ( $str )


Parameters: This function accepts a single parameter $str. It is a mandatory parameter which specifies the string.
Return Value: It returns True if a string contains only control characters and False on failure.
Examples: 
 

Input: GeeksforGeeks
Output: No
Explanation: String (GeeksforGeeks) contains only the alphanumeric characters.

Input: \n\t
Output: Yes
Explanation: String (\n\t) contains only the control character.


Below programs illustrate the ctype_cntrl() function.
Program 1: 
 

php
<?php
// PHP program to check if a string has all
// control characters

$str1 = "GeeksforGeeks";

    if ( ctype_cntrl($str1)) 
    
        echo "Yes\n";
    else
        echo "No\n";

$str2 = "\n\t";

    if ( ctype_cntrl($str2)) 
    
        echo "Yes\n";
    else
        echo "No\n";
?>

Output: 
No
Yes

 

Program 2: Implementation of ctype_cntrl() function which takes input of an array of string that contains integers and special Symbol. 
 

php
<?php
// PHP program to check if a string has all
// control characters
 
$str = array(
    "Geeks",
    "Geeks space",
    "@@##-- /",
    "\n",
    "\t \r",
    "\r\t\n"
);
 
// Check the above strings by using
// ctype_cntrl() function
foreach ($str as $test) {
     
    if (ctype_cntrl($test))
        echo "Yes\n";
    else
        echo "No\n";    
}
 
?>

Output: 
No
No
No
Yes
No
Yes

 

References: https://www.php.net/manual/en/function.ctype-cntrl.php
 


Practice Tags :

Similar Reads