I get the feeling that the answer is 'no', because I sure can't find it...

Is there a PHP equivalent to the typeof operator?

For instance, I want to know if $bar is an integer or not...

function foo($bar) {
if (typeof($bar) == integer) {
// do some stuff
} else {
echo('gimme an integer!');
exit;
}
} // end foo

Thanks in advance. I know this is a dumb noob question - it just seems like if this method doesn't exist, somebody's gotta have a hack for it by now..

    would isinteger work? (or is it is_integer()?)

      [man]is_integer[/man], [man]gettype[/man]

      but be careful:

      $a = 1;
      $b = "1";
      if (is_integer($a))
        echo '$a is an integer';
      else
        echo '$a is not an integer';
      echo '<br />';
      if (is_integer($b))
        echo '$b is an integer';
      else
        echo '$b is not an integer';
      echo '<br />';
      echo 'type of $a: '.gettype($a).'<br />';
      echo 'type of $b: '.gettype($b);

      output:

      $a is an integer
      $b is not an integer
      type of $a: integer
      type of $b: string

      maybe you're looking for [man]is_numeric[/man] or you want to convert the var to an integer:

      $a = '1';
      $b = (int)$a;

        is_integer()
        is_numeric()
        is_array()
        get_class()
        etc.

          I guess you could also use some form of a regular expression check for this as well ... define what you want to allow , then check against it

          $match = define the regular expression to check on here ..

          if(!$match) {
          // do stuff
          } else {

          // do stuff

          }

            3 years later

            Another alternative:

            if ($bar === (int)$bar) {
                // it's an integer
            } else {
                // not an integer
            }
              Write a Reply...