Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
ctype_graph
communitycreator

What is the ctype_graph method in PHP?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The ctype_graph method checks whether all the text characters are printable characters and create a visible output.

Syntax


ctype_graph(mixed $text): bool

Returns

The ctype_graph method returns a boolean value.

Non-printable characters in ctype_graph

Some common non-printable characters include:

  • \n: New line
  • \t: Horizontal tab
  • \r: Carriage return
  • \f: Form feed
  • \b: Backspace
  • \e: Escape
  • \v: Vertical tab

Refer here for a full list of non-printable characters.

Code

<?php
$str = "123";
printIsGraph($str);
$str = "acb!@#";
printIsGraph($str);
$str = "abc\n";
printIsGraph($str);
$str = "a b";
printIsGraph($str);
function printIsGraph($str){
echo "The string is: ". $str. "\n";
echo "Is Printable: ";
var_dump(ctype_graph($str));
echo "---------\n";
}
?>

Explanation

In the code above:

  • We use the ctype_graph method to check if the string contains only printable characters.

  • For the strings 123 and acb!@#, the ctype_graph method returns true.

  • For the strings abc\n and a b, the ctype_graph method returns false because the strings contain non-printable characters \n and whitespace, respectively.

RELATED TAGS

php
ctype_graph
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring