title: Best way to check if a variable is numeric
body: <p>I faced this problem multiple times and now I'm thinking about the best solution of the following problem:</p>
  
  <pre><code>public function dumpID($id){
      var_dump($id);
  }
  
  dump("5");
  </code></pre>
  
  <p>Will dump <code>string "5"</code>. Numeric values are often given to functions whatsoever as string. I'm facing this often in Symfony 2, but also in other, native, PHP projects.</p>
  
  <p>Checking if this ID is numeric would be failing in this case</p>
  
  <pre><code>public function dumpID($id){
      if(is_int($id)){
          var_dump($id);
      } else { // throw error }
  }
  
  dump("5"); // Would fail
  </code></pre>
  
  <p>So you could say <code>casting to int would solve this problem</code>.</p>
  
  <pre><code>public function dumpID($id){
      $id = (int)$id;
      if(is_int($id)){
          var_dump($id);
      } else { // throw error }
  }
  
  dump("5"); // Would var_dump the $id
  </code></pre>
  
  <p>But this isn't correct because of the following behaviour of PHP.</p>
  
  <pre><code>$string = "test";
  $string = (int)$string;
  var_dump($string); // Would give out the integer value 0
  </code></pre>
  
  <p>So</p>
  
  <pre><code>public function dumpID($id){
      $id = (int)$id;
      if(is_int($id)){
          var_dump($id);
      } else { // throw error }
  }
  
  // Would var_dump out the integer value 0 
  //and the if condition succeed even if the given value is a string!
  dump("test"); 
  </code></pre>
  
  <p>Which is problematic.</p>
  
  <p>So by now I have the following work-around:</p>
  
  <pre><code>public function dumpID($id){
      $id = (int)$id;
      if($id == (string)(int)$id){
          var_dump($id);
      } else { // throw error }
  }
  
  // Would throw an error because 0 != "test"
  dump("test"); 
  </code></pre>
  
  <p>Is there a better way to solve this problem, a way given in the core of PHP that I just don't know?</p>