Sometimes we need to display the size of a certain file or the size of other data on the web page.
This number often spans a large span. If it is in B, it may be a single digit. If it is 1G, it will be a number up to 1073741824. At this time, we need to format it according to the size. For example, if it is less than 1K, it will be displayed in B. If it is less than 1K, it will be displayed in B. 1M is displayed in KB, less than 1G is displayed in MB, and so on...
The formatting function reference is as follows:
<span>//</span><span>格式化size显示</span> <span>function</span> formatSize(<span>$b</span>,<span>$times</span>=0<span>){ </span> <span>if</span>(<span>$b</span>>1024<span>){ </span><span>$temp</span>=<span>$b</span>/1024<span>; </span><span>return</span> formatSize(<span>$temp</span>,<span>$times</span>+1<span>); }</span><span>else</span><span>{ </span><span>$unit</span>='B'<span>; </span><span>switch</span>(<span>$times</span><span>){ </span><span>case</span> '0':<span>$unit</span>='B';<span>break</span><span>; </span><span>case</span> '1':<span>$unit</span>='KB';<span>break</span><span>; </span><span>case</span> '2':<span>$unit</span>='MB';<span>break</span><span>; </span><span>case</span> '3':<span>$unit</span>='GB';<span>break</span><span>; </span><span>case</span> '4':<span>$unit</span>='TB';<span>break</span><span>; </span><span>case</span> '5':<span>$unit</span>='PB';<span>break</span><span>; </span><span>case</span> '6':<span>$unit</span>='EB';<span>break</span><span>; </span><span>case</span> '7':<span>$unit</span>='ZB';<span>break</span><span>; </span><span>default</span>: <span>$unit</span>='单位未知'<span>; } </span><span>return</span> <span>sprintf</span>('%.2f',<span>$b</span>).<span>$unit</span><span>; } }</span>
Call:
<span>echo</span> formatSize('20667564');
The result is:
19.71MB
Description:
The parameter $b is a number in B, and $times is used to identify how many times this function has been recursed.
For the uncommon units TB, PB, EB, and ZB, please refer to the notes below (sourced from the Internet):
1bit (this bit represents a binary number) 1Byte (this word is also called "bit" when transliterated but represents a hexadecimal number)
1B=1Byte=8bit
1 kB = 1024 B (kB - kilobyte) One MB = 1024 kB (MB - megabyte) One GB = 1024 MB (GB - gigabyte) One TB = 1024 GB (TB - terabyte) One PB = 1024 TB (PB - petabyte) 1 EB = 1024 PB (EB - eksabyte) Ai
1 ZB = 1024 EB (ZB - zettabyte) YB = 1024 ZB (YB - yottabyte) YB = 1024 YB (BB - brontobyte)
yotta, 尧[it], Y. 10^21,
zetta, ze[it], Z. 10^18,
exa, Ai[khazia], E. 10^15,
peta, shoot[it], P. 10 ^12,
tera, 太[拉], T. 10^9,
giga, 吉[卡], G. 10^6,
mega, trillion, M. 10^3
The above introduces the PHP format (file) storage data size (SIZE) display, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
