PHP - Function ctype_punct()
The PHP Character type checking ctype_punct() function is used to check whether the given string consists of all punctuation characters. The "punctuation " characters meaning it should not contain any letters, digits, or white space. The string must only include punctuation characters such as "&", "$", "()", and "^".
This function returns true if every character in the given string (text) is printable, but it should be neither letter, digit, or blank; 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_punct() function −
ctype_punct(mixed $text): bool
Parameters
Following is the parameter of this function −
- text (required) − A string needs to be tested.
Return Value
This function returns "true", if every character in the text is printable, and "false" otherwise.
Example 1
The following is the basic example of the PHP ctype_punct() function −
<?php $string = "*&$()^"; echo "The given string is: $string"; echo "\nDoes the string '$string' consist of all punctuation? "; var_dump(ctype_punct($string)); ?>
Output
Following is the output of the above program −
The given string is: *&$()^ Does the string '*&$()^' consist of all punctuation? bool(true)
Example 2
If the given string contains any printable characters that are white-space or alphanumeric (letters and digits), the PHP ctype_punct() function will return false −
<?php $string = "Tutorialspoint!#@!!&"; echo "The given string is: $string"; echo "\nDoes the string '$string' consist of all punctuation? "; var_dump(ctype_punct($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 punctuation? bool(false)
Example 3
If the given string (text) is empty (""), this function always will return false −
<?php $string = ""; echo "The given string is: $string"; echo "\nDoes the string '$string' consist of all punctuation? "; var_dump(ctype_punct($string)); ?>
Output
Once the above program is executed, it generates the following output −
The given string is: Does the string '' consist of all punctuation? bool(false)