STRINGS

Check String Value is Number

This code Check String Value is Number or not and Prints the value accordingly.

About the Code:

  • Check String Value is Number.
  • This code will check if the input value is number or not.
  • Execute this code in console using > php <filename>.
  • We will use is_numeric() method to check if input is number.
  • PHP_EOL is used to print new line on the console.

    $strTotal1 = "100";
    $strTotal2 = "100 Marks";

    echo "Input Values Total1: $strTotal1 and  Total1: $strTotal2" . PHP_EOL;

    if( is_numeric( $strTotal1 ) ) {
        echo "Input value Total1: $strTotal1 is Number" . PHP_EOL;
    }else{
        echo "Input value Total1: $strTotal1 is NOT Number" . PHP_EOL;
    }


    if( is_numeric( $strTotal2 ) ) {
        echo "Input value Total2: $strTotal2 is Number" . PHP_EOL;
    }else{
        echo "Input value Total2: $strTotal2 is NOT Number" . PHP_EOL;
    }

Output:


Input Values Total1: 100 and  Total1: 100 Marks
Input value Total1: 100 is Number
Input value Total2: 100 Marks is NOT Number


Other Similar Functions:

  • is_null() – Finds whether a variable is NULL
  • is_float() – Finds whether the type of a variable is float
  • is_int() – Find whether the type of a variable is integer
  • is_string() – Find whether the type of a variable is string
  • is_object() – Finds whether a variable is an object
  • is_array() – Finds whether a variable is an array
  • is_bool() – Finds out whether a variable is a boolean
Tags

About the author

PHPTraining.com

Do you know we teach Free PHP Course which has tons of Exercise, Assignments, Code Editor, Quiz and Certification. Access the course from here.

If you liked this article, then please subscribe to our YouTube Channel for PHP video tutorials. You can also find us on Twitter and Facebook.