PHP ctype_space() Function (With Examples)

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

PHP ctype_space() function

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

Note

Whitespace contains some of the other things like "tabs" (horizontal, vertical), "line feed", "carriage return", and "form feed".

Syntax

The syntax of the ctype_space() function:

ctype_space(string) : bool

Parameters

The parameters of the ctype_space() function:

  • string: An input string.

Return Value

The return type of this method is bool, it returns true – if the string contains whitespaces, else it returns false.

Sample Input/Output

Input: "\r\n"
Output: true

Input: " "
Output: true

Input: "Hello world"
Output: false

PHP ctype_space() Function Example

<?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

The output of the above example is:

  contains whitespaces.

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

To understand the above example, you should have the basic knowledge of the following PHP topics:


Comments and Discussions!

Load comments ↻






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