PHP ctype_punct() Function (With Examples)

In this tutorial, we will learn about the PHP ctype_punct() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP ctype_punct() function

The ctype_punct() function is a character type (CType) function in PHP, it is used to check whether a given string contains all punctuation characters (can be considered as special characters) or not.

Syntax

The syntax of the ctype_punct() function:

ctype_punct(string) : bool

Parameters

The parameters of the ctype_punct() function:

  • string: An input string.

Return Value

The return type of this method is bool, it returns true, if all characters of the given strings are punctuation characters, else it returns false.

Sample Input/Output

Input: "@!~#"
Output: true

Input: "@!  #~"
Output: false

Input: "Hello@!"
Output: false

PHP ctype_punct() Function Example

<?php
    $str = "@!~#";
    if(ctype_punct($str))
        echo ("$str contains all punctuation characters.\n");
    else
        echo ("$str does not contain all punctuation characters.\n");

    $str = "@!  #~"; //space is there
    if(ctype_punct($str))
        echo ("$str contains all punctuation characters.\n");
    else
        echo ("$str does not contain all punctuation characters.\n");

    $str = "HELLO@!"; //alphabets are there
    if(ctype_punct($str))
        echo ("$str contains all punctuation characters.\n");
    else
        echo ("$str does not contain all punctuation characters.\n");

    $str = "@!123"; //digits are there
    if(ctype_punct($str))
        echo ("$str contains all punctuation characters.\n");
    else
        echo ("$str does not contain all punctuation characters.\n");
?>

Output

The output of the above example is:

@!~# contains all punctuation characters.
@!  #~ does not contain all punctuation characters.
HELLO@! does not contain all punctuation characters.
@!123 does not contain all punctuation characters. 

To understand the above example, you should have the basic knowledge of the following PHP topics:

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.