//根据题意设定初始变量值//然后 一个while循环去操作//每换得一个瓶子,换的那个减去要做相应的数目//然后喝了啤酒后, 各数量加1//直到不符合要求。跳出循环 1 class Beer{ 2 3 protected $uni_gai = 4; //每4个瓶盖1瓶 4 protected $uni_bottle = 2; //每两个瓶子换1瓶 5 protected $uni_beer = 2; //每瓶2块钱 6 protected $rs = array();//存取结果 7 protected $total = 0; //当前买了啤酒的数量 8 protected $gai = 0; //当前有多少个啤酒 9 protected $empty_bottle = 0; //空瓶子10 11 public function __construct($money){12 $cur = $money / $this->uni_beer;13 $this->total = $cur;14 $this->gai = $cur;15 $this->empty_bottle = $cur;16 }17 18 public function run(){19 while($this->gai > 0 || $this->empty_bottle > 0){20 if($this->gai >= $this->uni_gai){21 $this->deal_num('gai');22 }23 if($this->empty_bottle >= $this->uni_bottle){24 $this->deal_num('empty_bottle');25 }26 27 $this->check_overflow();28 }29 return $this->rs;30 }31 32 public function deal_num($type){33 if($type == 'gai'){34 $this->gai -= $this->uni_gai;35 }else{36 $this->empty_bottle -= $this->uni_bottle;37 }38 $this->gai++;39 $this->empty_bottle++;40 $this->total++;41 }42 public function check_overflow(){43 if($this->gai < $this->uni_gai && $this->empty_bottle < $this->uni_bottle){44 $this->rs['gai'] = $this->gai;45 $this->rs['total'] = $this->total;46 $this->rs['empty_bottle'] = $this->empty_bottle;47 $this->gai = 0;48 $this->empty_bottle = 0;49 }50 }51 public function _print(){52 echo 'gai:', $this->gai;53 echo '<br>';54 echo 'empty_bottle:', $this->empty_bottle;55 echo '<br>';56 echo 'total', $this->total;57 echo '<hr>';58 }59 }60 61 $peer = new Beer(10);62 $rs = $peer->run();63 print_r($rs);
打印的结果是: Array ( [gai] => 3 [empty_bottle] => 1 [total] => 15 )
盖子 3个, 空瓶子 1个, 总共喝了15瓶啤酒

tomakephpapplicationsfaster,关注台词:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

到ImprovephPapplicationspeed,关注台词:1)启用opcodeCachingwithapCutoredUcescriptexecutiontime.2)实现databasequerycachingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandredececonnection.4 limitsclection.4.4

依赖注入(DI)通过显式传递依赖关系,显着提升了PHP代码的可测试性。 1)DI解耦类与具体实现,使测试和维护更灵活。 2)三种类型中,构造函数注入明确表达依赖,保持状态一致。 3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

databasequeryOptimizationinphpinvolVolVOLVESEVERSEVERSTRATEMIESOENHANCEPERANCE.1)SELECTONLYNLYNESSERSAYCOLUMNSTORMONTOUMTOUNSOUDSATATATATATATATATATATRANSFER.3)

phpisusedforsenderemailsduetoitsbuilt-inmail()函数andsupportiveLibrariesLikePhpMailerandSwiftMailer.1)usethemail()functionforbasicemails,butithasimails.2)butithasimimitations.2)

PHP性能瓶颈可以通过以下步骤解决:1)使用Xdebug或Blackfire进行性能分析,找出问题所在;2)优化数据库查询并使用缓存,如APCu;3)使用array_filter等高效函数优化数组操作;4)配置OPcache进行字节码缓存;5)优化前端,如减少HTTP请求和优化图片;6)持续监控和优化性能。通过这些方法,可以显着提升PHP应用的性能。

依赖性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增强量产生性,可验证性和Maintainability.itallowspasspassingDepentenciesLikEdenceSeconnectionSeconnectionStoclasseconnectionStoclasseSasasasasareTers,interitationApertatingAeseritatingEaseTestingEasingEaseTeStingEasingAndScalability。

cachingimprovesphpermenceByStorcyResultSofComputationsorqucrouctationsorquctationsorquickretrieval,reducingServerLoadAndenHancingResponsetimes.feftectivestrategiesinclude:1)opcodecaching,whereStoresCompiledSinmememorytssinmemorytoskipcompliation; 2)datacaching datacachingsingMemccachingmcachingmcachings


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能