PHP ctype_space() function with example

PHP ctype_space() work: Here, we will find out about the ctype_space() work with example in PHP.

PHP ctype_space() work

ctype_space() work is a character type (CType) work in PHP, it is utilized to check whether a given string contains whitespace or not.

Note: Whitespace contains a portion of different things like “tabs” (flat, vertical), “line feed”, “carriage return”, and “structure feed”.

It returns genuine – if the string contains whitespaces, else it returns false.

Syntax:

    ctype_space(string) : bool

Output:

    Input: "\r\n"
    Output: true
    Input: " "
    Output: true
    Input: "Hello world"
    Output: false

PHP Code:

<?php
    $str = " ";
    if(ctype_space($str))
        echo ("$str contains whitespaces.\n");
    else
        echo ("$str does not contain whitespaces.\n");

    $str = "\r\n";
    if(ctype_space($str))
        echo ("$str contains whitespaces.\n");
    else
        echo ("$str does not contain whitespaces.\n");

    $str = "Hello world123";
    if(ctype_space($str))
        echo ("$str contains whitespaces.\n");
    else
        echo ("$str does not contain whitespaces.\n");

    $str = "abc@123&a";
    if(ctype_space($str))
        echo ("$str contains whitespaces.\n");
    else
        echo ("$str does not contain whitespaces.\n");
?>

Output:

  contains whitespaces.

 contains whitespaces.
Hello world123 does not contain whitespaces.
abc@123&a does not contain whitespaces.

Leave a Comment