A simple introduction to the PHP implementation of byte conversion to KB, MB, GB, TB method

Source: Internet
Author: User
This article mainly introduces the PHP implementation of byte-byte conversion to KB, MB, GB, TB method, combined with an instance of PHP for the byte count of the conversion operation of the specific implementation method, involving the use of PHP mathematical operations related functions, the need for friends can refer to the next

This article describes the PHP implementation of byte bytes converted to KB, MB, GB, TB methods. Share to everyone for your reference, as follows:

The Java implementation of byte-to-KB, MB, GB, and TB methods is described earlier, and PHP is used to implement this function instead. The code is simple:


<?phpfunction GetFileSize ($num) {   $p = 0;   $format = ' bytes ';   if ($num >0 && $num <1024) {     $p = 0;     Return Number_format ($num). ' '. $format;   }   if ($num >=1024 && $num <pow (1024x768, 2)) {     $p = 1;     $format = ' KB ';  }  if ($num >=pow (1024x768, 2) && $num <pow (1024x768, 3)) {    $p = 2;    $format = ' MB ';  }  if ($num >=pow (1024x768, 3) && $num <pow (1024x768, 4)) {    $p = 3;    $format = ' GB ';  }  if ($num >=pow (1024x768, 4) && $num <pow (1024x768, 5)) {    $p = 3;    $format = ' TB ';  }  $num/= Pow (1024x768, $p);  Return Number_format ($num, 3). ' '. $format;} echo "www.jb51.net test results from the home of the script:<br/>"; Echo getfilesize (200). " <br/> "; Echo getfilesize (20000)." <br/> "; Echo getfilesize (2000000)." <br/> "; Echo getfilesize (200000000)." <br/> "; Echo getfilesize (20000000000)." <br/> "; Echo getfilesize (2000000000000)." <br/> ";? >

Running results such as:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.