Home >Backend Development >PHP Tutorial >An elegant byte-related conversion formula

An elegant byte-related conversion formula

WBOY
WBOYOriginal
2016-07-25 09:10:51911browse

Function analysis

  1. /**
  2. * @desc Convert to bytes
  3. * Pay attention to the usage of case. Since break; is not written, an elegant conversion formula can be implemented
  4. * But if the order is changed, it will be over.
  5. * @author wangjiafang
  6. * @since 2010.12 .22
  7. * @param string
  8. * @return string
  9. */
  10. function return_bytes($val) {
  11. $val = trim($val);
  12. $last = strtolower($val{strlen($val)-1});
  13. switch($last) {
  14. case 'g': $val *= 1024;
  15. case 'm': $val *= 1024;
  16. case 'k': $val *= 1024;
  17. }
  18. return $val;
  19. }
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Send email with PHPNext article:Send email with PHP