Home » PHP » PHP Ctype functions

PHP ctype_digit() function with example

PHP ctype_digit() function: Here, we are going to learn about the ctype_digit() function with example in PHP.
Submitted by IncludeHelp, on February 04, 2019

PHP ctype_digit() function

ctype_digit() function is a character type (CType) function in PHP, it is used to check whether a given string contains only digits or not.

It returns true – if the string contains only digits (0...9), else it returns false.

Syntax:

    ctype_digit(string) : bool

Example:

    Input: "1234"
    Output: true
    Input: "Hello123"
    Output: false
    Input: "[email protected]&a"
    Output: false

PHP code:

<?php
    $str = "123456";
    if(ctype_digit($str))
        echo ("$str contains only digits.\n");
    else
        echo ("$str does not contain only digits.\n");

    $str = "123.456";
    if(ctype_digit($str))
        echo ("$str contains only digits.\n");
    else
        echo ("$str does not contain only digits.\n");

    $str = "Hello world123";
    if(ctype_digit($str))
        echo ("$str contains only digits.\n");
    else
        echo ("$str does not contain only digits.\n");

    $str = "[email protected]&a";
    if(ctype_digit($str))
        echo ("$str contains only digits.\n");
    else
        echo ("$str does not contain only digits.\n");
?>

Output

123456 contains only digits.
123.456 does not contain only digits.
Hello world123 does not contain only digits.
[email protected]&a does not contain only digits.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.