• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2928
  • Last Modified:

Warning number_format() expects parameter 2 to be long, string given on line 4

I see this warning message every time a user decides to "use commas to separate numbers" in my application. How do I have to change this code so it will also accept commas?
Thank you

  if($this->d->is_stealth=='0' && !$this->d->use_comma)
  imagestring($this->im,3,$locx,$locy,$this->d->hits,$this->w);
  elseif($this->d->is_stealth=='0' && $this->d->use_comma)
 imagestring($this->im,3,$locx,$locy,number_format($this->d->hits,','),$this->w);

Open in new window

0
seopti
Asked:
seopti
1 Solution
 
GaryCommented:
How should the numbers look?  Could you not just do a replace on the input before processing?
0
 
Ray PaseurCommented:
This is almost the "dictionary picture" of incomprehensible code.  Can you please tell us in plain language what you're trying to do?  If we know your intentions we can almost always help with a programming solution.  Maybe post a link to the page involved?  And a simple explanation of the code or maybe some comments to show the "why" parts?  Thanks, ~Ray
0
 
seoptiAuthor Commented:
This is a free website counter. There is a .png image. Users can choose in the admin if they want the counter image displayed with or without comma. If they choose to display with comma the counter image won't appear at all.  There is no problem without a comma.

This is the whole function.

 function show_counter()
  {
    $this->c=mysql_query("SELECT t1.hits,t1.counter_type,t1.use_cookie,"
    . "t1.use_comma,t1.ban_ip,t2.filename,t2.xpos,t2.ypos,"
    . "t2.is_stealth "
    . "FROM CC_counter AS t1 INNER JOIN CC_image AS t2 "
    . "ON t1.counter_type = t2.id WHERE t1.id='$this->uid'");
    $this->d=mysql_fetch_object($this->c);
    if(is_object($this->d))
    {        
      $this->image = "images/" . $this->d->filename . ".png";

      $this->d->ban_ip=stripslashes($this->d->ban_ip);

      if($this->d->ban_ip!=$this->ip)
        if(!$this->cookie_in_use)
          ++$this->d->hits;

      header('P3P: CP=\"NOI DSP COR NID ADMa SAMa BUS COM STA\"');
      header('Content-type: text/plain'); 

echo $row['image'];

     
      if($this->d->ban_ip!=$this->ip)
      {
        if(!$this->cookie_in_use)
        {

      mysql_query("UPDATE CC_counter SET last_access=".time().",hits='".$this->d->hits."' WHERE id='$this->uid'");
          
            if(!$this->name)
              $this->name=" ";
            if(!$this->os)
              $this->os=" ";
            if(!$this->sw)
              $this->sw=" ";
            if(!$this->sd)
            $this->sd=" ";

      include("/usr/www/users/config.inc.php");
      if (isset($plugin_ip2country) && $plugin_ip2country != "no"){
        include("plugins/ip2country/ip2country.inc.php");
        $country = getCountry($_SERVER["REMOTE_ADDR"]);
        $country = $country[1];
      }

      mysql_query("INSERT INTO CC_visitor(counter,time,referer,ip,browser,os,screen,depth,spare1,spare2,spare3) "
	. "VALUES ($this->uid,'".time()."','$this->referer2','" . $this->ip . "','$this->name','$this->os',"
	. "'$this->sw','$this->sd','$this->key','$this->engine', '$country')");
      $visitorID = mysql_insert_id();

    if ($this->page)
      mysql_query("INSERT DELAYED INTO CC_track (counter,visitor,time,url) "
    . "VALUES ($this->uid,'$visitorID','" . time() . "','".$this->page."')");
        } else {
    //record the visitors track
    if ($this->cookie_in_use > 0 && $this->page){
      $visitorID = $this->cookie_in_use;
      mysql_query("INSERT INTO CC_track (counter,visitor,time,url) "
    . "VALUES ($this->uid,'$visitorID','" . time() . "','".$this->page."')");
    }
  }
      }

      if($this->d->ban_ip!=$this->ip)
        mysql_query("update CC_counter set totalhits=totalhits+1 where id='$this->uid'");

      if (!$visitorID)
        $visitorID = "Unknown";
      setcookie("CC_COUNTER_CHECK_$this->uid",$visitorID,time()+86400);

      $this->im=imagecreatefrompng($this->image);
      $this->w=imagecolorallocate($this->im, 0, 0, 0);
      
      if(!($locx=($this->d->xpos))){
        $locx = 10;
      }
      
      if(!($locy=($this->d->ypos))){
        $locy = 10;
      }
      
      if($this->d->is_stealth=='0' && !$this->d->use_comma)
        imagestring($this->im,3,$locx,$locy,$this->d->hits,$this->w);
      elseif($this->d->is_stealth=='0' && $this->d->use_comma)
        imagestring($this->im,3,$locx,$locy,number_format($this->d->hits,','),$this->w);
        
      imagepng($this->im);
      flush();
      imagedestroy($this->im);
 
    }
    else
    {
      $this->bad_user();
    }
  }


  function bad_user()
  {
    
    exit;
  }
}

Open in new window

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Marco GasiFreelancerCommented:
Lok at the manual: http://www.php.net/number_format.
The function expects at least one parameter: float $number .
The other ones are optionals, but... If you need to use the third parameter you must pass the secon also, even if it is optional, since php function look at the parameters order to identify them.
So, if you don't worry about how many decimals the number has to have, you must take this decision anyway:

if($this->d->is_stealth=='0' && !$this->d->use_comma)
{
    imagestring($this->im,3,$locx,$locy,$this->d->hits,$this->w);
}
elseif ($this->d->is_stealth=='0' && $this->d->use_comma)
{
    imagestring($this->im,3,$locx,$locy,number_format($this->d->hits, 3,','),$this->w);
}

Open in new window


Cheers
0
 
Chris StanyonCommented:
The default for number_format is to use commas, so you don't need to specify it. Use this:

number_format($this->d->hits)

The reason you're getting an error is because the second parameter of your call to number_format is ',' which is a string. The second argument is for the number of decimals you want, and should be a number. The thousand separator, if you need to specify it is the 4th argument (the third is the decimal separator):

number_format($number, 2, '.', ',');

@Marco - number_format doesn't take 3 parameters - it takes 1, 2 or 4 !!
0
 
Ray PaseurCommented:
@seopti: For future reference, you may want to know where to look when you get messages associated with a PHP function.  All of the PHP functions are documented in the online man pages.  You can go to the URL http://php.net where you will find a search box in the upper right corner.  Type the name of the function, eg: number_format() into that search box and search the PHP web site.  It will take you straight to the function's web page, where you will find the description, the parameters, the return values usage examples and perhaps most importantly, user-contributed notes showing how other programmers have used the function to solve a particular problem.  The documentation is available in many languages.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now