0

I am using is_int function To check Whether The Value i get is int or not My code is

<?php

$a = 3;

if(is_int($a)){
    echo "INT";
}else{
    echo "STRINIG";
}

Which returns me INT But When it tried Using This code

<?php

$a = "3";

if(is_int($a)){
    echo "INT";
}else{
    echo "STRINIG";
}

It returns me sting .

So why this happen ?

UPDATE

I have problem in following code Let Suppose i have array As

Array ( [0] => 4 [1] => xxxx )

now i have to fetch String So i do

foreach ($mypreferdservice as $value) {
  if(is_int($value)){
    echo "Int<br>";
  }else{
    echo "String";
  }
}

So it returns me string So How to solve this problem

6
  • 1
    well, the function is checking the type of variable. and as $a variable assigned with a string value, its type is string. Commented Feb 3, 2018 at 11:10
  • But how can Only Double Quote(") Change Variable Type ? Commented Feb 3, 2018 at 11:11
  • 1
    "3" is a string while 3 is a int. Commented Feb 3, 2018 at 11:12
  • PHP is dynamically typed language, meaning that type of variable defined by its content. Going deeper in the memory 3 and "3" even represented differently. If you read the docs: "To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric()." Commented Feb 3, 2018 at 11:12
  • @WebInfos, That's how string type works in dynamic type language. You can set any type value to a variable. Commented Feb 3, 2018 at 11:13

3 Answers 3

5

If you want check if the value is numeric, then is_numeric(http://php.net/manual/en/function.is-numeric.php) may be what your after -

$a = "3";

if(is_int($a)){
    echo "INT";
}else{
    echo "STRINIG";
}
if(is_numeric($a)){
    echo "INT";
}else{
    echo "STRINIG";
}

is_int checks the type of a field, is_numeric checks the value.

This outputs (not formatted very well)...

STRINIGINT
Sign up to request clarification or add additional context in comments.

Comments

1

Anything surrounded by " or ' is string typed. That's why is_int function returning false.

The simplest way to specify a string is to enclose it in single quotes (the character ').

If the string is enclosed in double-quotes ("), PHP will interpret the following escape sequences for special characters:

php.net wiki on string type

To get an idea how php data types work, read on php data types

Comments

-1

If you want to check if a string is an integer number then is_int or is_numeric alone won't cut it:

function is_integer_numeric($string) {
           return is_numeric($string) && is_int($string + 0); // Adding 0 to a numeric will cast it to either a float or int depending on what it really is.
}


var_dump(is_integer_numeric(3)); // true 
var_dump(is_integer_numeric("3")); // true
var_dump(is_integer_numeric("3.1")); // false
var_dump(is_integer_numeric(3.1));  // false
var_dump(is_integer_numeric("test"));  // false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.