PHP ctype_graph() Function (With Examples)

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

PHP ctype_graph() function

The ctype_graph() function is a character type (CType) function in PHP, it is used to check whether a given string contains all printable characters (except space) or not.

Printable characters are the characters that can be printed on the screen. The all alphabets, digits, whitespace and special characters are the printable characters. But, this function does not consider space as a printable character.

Syntax

The syntax of the ctype_graph() function:

ctype_graph(string) : bool

Parameters

The parameters of the ctype_graph() 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 printable characters except whitespace. Else it returns false.

Sample Input/Output

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 ctype_graph() Function Example

<?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

The output of the above example is:

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.

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.