PHP - Function ctype_graph()



The PHP Character type checking ctype_graph() function is used to determine whether a given string consists of all (visibly) printable characters. A "printable" character refers to those characters that will be visible in the output.

This function returns a boolean value true if the given string consists only of (visibly) printable 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_graph() function −

ctype_graph(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 printable, and "false" otherwise.

Example 1

The following program demonstrates the usage of the PHP ctype_graph() function −

<?php
   $string = "HelloWorld";
   echo "The given string is: $string";
   echo "\nDoes the string is printable? ";
   var_dump(ctype_graph($string));
?>

Output

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

The given string is: HelloWorld
Does the string is printable? bool(true)

Example 2

If every character in the given text is not printable, the PHP ctype_graph() function returns false

<?php
   $string = "Tutorialpoint\t";
   echo "The given string is: $string";
   echo "\nDoes the string is printable? ";
   var_dump(ctype_graph($string));
?>

Output

The above program produces the following output −

The given string is: Tutorialpoint
Does the string is printable? bool(false)

Example 3

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

<?php
   $string = "";
   echo "The given string is: $string";
   echo "\nDoes the string is printable? ";
   var_dump(ctype_graph($string));
?>

Output

Once the above program is executed, it generates the following output −

The given string is:
Does the string is printable? bool(false)

Example 4

Checking multiple strings (texts).

In the example below, we use the PHP ctype_graph() function to check whether the provided strings consist of all (visibly) printable characters and use the "for-each" loop to traverse through the given array of strings −
<?php
   $string = array("Tutorialspoint", "India\t//r", "Hello@3&");
   echo "The given strings are: ";
   foreach($string as $text){
	   echo $text." ";
   }
   foreach($string as $test){
	   if(ctype_graph($test)){
		   echo "\nThe string '$test' consists of all (visibly) printable characters";
	   }
	   else{
		   echo "\nThe string '$test' does not consist of all (visibly) printable characters";
	   }
   }
   
?>

Output

Following is the output of the above program −

The given strings are: Tutorialspoint India     //r Hello@3&
The string 'Tutorialspoint' consists of all (visibly) printable characters
The string 'India      //r' does not consist of all (visibly) printable characters
The string 'Hello@3&' consists of all (visibly) printable characters
php_function_reference.htm
Advertisements