Solved

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

Posted on 2014-02-22
6
2,184 Views
Last Modified: 2014-02-23
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
Comment
Question by:seopti
6 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39880051
How should the numbers look?  Could you not just do a replace on the input before processing?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39880053
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
 

Author Comment

by:seopti
ID: 39880076
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39880249
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
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39880486
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39880566
@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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now