Content · Programming · PHP

PHP ctype_graph() Function: Usage and Examples

Learn how PHP ctype_graph works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Checks for printable characters other than whitespace.

What Is ctype_graph() in PHP?

Checks for printable characters other than whitespace.

Its syntax is:

ctype_graph(mixed $text): bool

Parameters:

  • $text — Accepted as mixed; required argument.

Return type: bool. An empty string returns false. These functions examine bytes and locale classification, not full Unicode character properties.

Availability: Requires the ctype extension, normally bundled with PHP.

Example 1: Basic Usage

<?php

var_dump(ctype_graph("Code!"));

Output:

bool(true)

An empty string returns false. These functions examine bytes and locale classification, not full Unicode character properties.

Example 2: A Different Input

<?php

var_dump(ctype_graph("A B"));

Output:

bool(false)

An empty string returns false. These functions examine bytes and locale classification, not full Unicode character properties.

Practical Example: Applying It in a Program

<?php

$samples = ["A1", "AB", "12", "A-1"]; foreach ($samples as $sample) { echo $sample . ": " . (ctype_graph($sample) ? "yes" : "no") . PHP_EOL; }

Output:

A1: yes
AB: yes
12: yes
A-1: yes

An empty string returns false. These functions examine bytes and locale classification, not full Unicode character properties.

Important Behavior and Edge Cases

  • Key behavior: An empty string returns false. These functions examine bytes and locale classification, not full Unicode character properties.
  • Environment: Requires the ctype extension, normally bundled with PHP.

Comparison With Related Functions

FunctionPurpose
ctype_graph()Checks for printable characters other than whitespace.
preg_match()preg_match() evaluates a regular-expression pattern; ctype_* functions test every byte against a predefined character class.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: An empty string returns falseAn empty string returns false. These functions examine bytes and locale classification, not full Unicode character properties.

Key Takeaways

  • Checks for printable characters other than whitespace.
  • An empty string returns false. These functions examine bytes and locale classification, not full Unicode character properties.
  • Requires the ctype extension, normally bundled with PHP.

Summary: An empty string returns false. These functions examine bytes and locale classification, not full Unicode character properties.

Share this resourceSend this page to someone who may find it useful.