PHP ctype_print() Function (With Examples)

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

PHP ctype_print() function

The ctype_print() function is a character type (CType) function in PHP, it is used to check whether a given string contains all printable characters 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.

Syntax

The syntax of the ctype_print() function:

ctype_print(string) : bool

Parameters

The parameters of the ctype_print() 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. Else it returns false.

Sample Input/Output

Input: "Hello 123"
Output: true

Input: "I am 123@#!~*.()"
Output: true

Input: "Hello\r\nWorld"
Output: false

Input: "Hello\x41"
Output: true

PHP ctype_print() Function Example

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

    $str = "I am 123@#!~*.()";
    if(ctype_print($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_print($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_print($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:

Hello 123 contains all printable characters.
I am 123@#!~*.() contains 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:

All PHP Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.