PHP ctype_graph() function with example

PHP ctype_graph() work: Here, we will find out about the ctype_graph() work with example in PHP.

PHP ctype_graph() work

ctype_graph() work is a character type (CType) work in PHP, it is utilized to check whether a given string contains every single printable character (with the exception of the room) or not.

It returns genuine if all characters of the given strings are printable characters aside from whitespace. Else it returns false.

Printable characters are the characters that can be imprinted on the screen. The all letters in order, digits, whitespace and unique characters are the printable characters. Be that as it may, this capacity doesn’t think about space as a printable character.

Syntax:

    ctype_graph(string) : bool

Example:

    Input: "Hello123"     Output: true     Input: "Iam123@#!~*.()"     Output: true     Input: "Hello world!"     Output: false     Input: "Hello\r\nWorld"     Output: false     Input: "Hello\x41"     Output: true     Input: "Hello123"
    Output: true
    Input: "Iam123@#!~*.()"
    Output: true
    Input: "Hello world!"
    Output: false
    Input: "Hello\r\nWorld"
    Output: false
    Input: "Hello\x41"
    Output: true

PHP Code:

<?php
    $str = "Hello123";
    if(ctype_graph($str))
        echo ("$str contains all printable characters.\n");
    else
        echo ("$str does not contain all printable characters.\n");

    $str = "Iam123@#!~*.()";
    if(ctype_graph($str))
        echo ("$str contains all printable characters.\n");
    else
        echo ("$str does not contain all printable characters.\n");

    $str = "Hello World";
    if(ctype_graph($str))
        echo ("$str contains all printable characters.\n");
    else
        echo ("$str does not contain all printable characters.\n");
	
    $str = "Hello\r\nWorld"; //"\r" and "\n" are not printable characters
    if(ctype_graph($str))
        echo ("$str contains all printable characters.\n");
    else
        echo ("$str does not contain all printable characters.\n");

    $str = "Hello\x41"; //"\x41" is the ASCII value of "A"
    if(ctype_graph($str))
        echo ("$str contains all printable characters.\n");
    else
        echo ("$str does not contain all printable characters.\n");	
?>

Output:

Hello123 contains all printable characters.
Iam123@#!~*.() contains all printable characters.
Hello World does not contain all printable characters.
Hello
World does not contain all printable characters.
HelloA contains all printable characters.

Leave a Comment