PHP - Function ctype_cntrl()



The PHP Character type checking ctype_cntrl() function is used to check if all characters in a given string are control characters. Control characters are non-printable characters that control how text is processed or displayed, such as newline (\n), tab (\t), and others.

This function returns a boolean value true if the given string consists only of control characters; otherwise, it returns false. If the given string is empty (""), this function always will return "false".

Syntax

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

ctype_cntrl(mixed $text): bool

Parameters

This function accepts a single parameter, which is described below −

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

Return Value

This function returns "true" if every character in the text is a control character, and "false" otherwise.

Example 1

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

<?php
   $string = "\t\n";
   echo "The given string is: $string";
   echo "\nDoes the string '$string' consists of all control characters? ";
   var_dump(ctype_cntrl($string));
?>

Output

The above program produces the following output −

The given string is:

Does the string '
' consists of all control characters? bool(true)

Example 2

If the given string does not consist of all control characters, the PHP ctype_cntrl() function will return false

<?php
   $string = "Tutorialspoint";
   echo "The given string is: $string";
   echo "\nDoes the string '$string' consist of all control characters? ";
   var_dump(ctype_cntrl($string));
?>

Output

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

The given string is: Tutorialspoint
Does the string 'Tutorialspoint' consist of all control characters? bool(false)

Example 3

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

<?php
   $string = " ";
   echo "The given string is: $string";
   echo "\nDoes the string '$string' consist of all control characters? ";
   var_dump(ctype_cntrl($string));
?>

Output

Following is the output of the above program −

The given string is:
Does the string ' ' consist of all control characters? bool(false)
php_function_reference.htm
Advertisements