Jump to content

Convert IP to dottless decimal


christa

Recommended Posts

hi all

why this function return 0 and NOT 3573956936?

 

$ip = '213.6.65.72';
function ip2dec($ip) {
  $base=explode(".", $ip);
  $decimal=(double)$base[0]*16777216;
  $decimal+=$base[1]*65536;
  $decimal+=$base[2]*256;
  $decimal+=$base[3];
  if($decimal>2147483647) {
    $decimal-=4294967296;
  }
  return (int)$decimal;
}
echo ip2dec();

Link to comment
https://forums.phpfreaks.com/topic/165845-convert-ip-to-dottless-decimal/
Share on other sites

And you've got to pass the function a value when you call it. 0 in = 0 out.

how can i call my function to display the correct ip dottless?

 

dotless? str_replace(".","",$ip);

however, what they said was if you do function(); it's the same as function(0); which in turn, 0 "mathed" against anything will return 0 (which is why you get 0!)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.