PHP第五课 自动类型转换与流程控制
学习概要:
1.了解自动类型转换的有哪些
2.了解基本的流程控制语句
3.实例:实现日历表格的写法
自动类型转换
1)整型转字符串echo $num."abc";
2)字符串转整型
$str+3;
3)转布尔类型
为假情况 0 "" "0" false array() null 未定义
4)强制类型转换
(int)$str (float)$str (string)$str
5)常量
define("HOST","localhost");
6)运算符
①一元
++ --
②二元
= - * / %
= += -= *= /= %=
> >= && || !
③三元
? :
流程控制:
1.流程控制
2.函数
流程控制:
一.分支结构
if...elseif....else
switch...case
如果条件是固定值的话,就用switch语句
二.循环控制
for
while
三.中止循环
break:直接结束
<?PHP header("content-type:text/html;charset=utf-8"); for($i=1;$i<10;$i++){ if($i==3){ break; } else{ echo $i."<br>"; } } ?>//1 2
continue:结束本次循环
<?PHP header("content-type:text/html;charset=utf-8"); for($i=1;$i<10;$i++){ if($i==3){ continue; } else{ echo $i."<br>"; } } ?>//1 2 4 5 6 7 8 9
小技巧:exit表示暂停下面的程序
echo date("w"); date 中w表示星期几 <?PHP header("content-type:text/html;charset=utf-8"); echo date("Y-M-D");//分别表示年月日 exit; echo "John"; ?>
五.剩余部分
1.do...while
<?php $score=31; do{ echo "<h1 id="score">{$score}</h1>"; }while($score>=60); ?>
4.九九乘法表
<?php for($i=1;$i<=9;$i++){ for($j=1;$j<=$i;$j++){ echo "$i*$j=".$i*$j." "; } echo "<br>"; } ?>
3.PHP实现日历表格
日历表格:
1.两层for循环
2.隔行换色
3.用到if条件判断
4.header头改编码
<?php header("content-type:text/html;charset=utf-8"); $days= 31; echo "<table width='700px' border='1px'>"; for($i=1;$i<=$days;){ echo "<tr>"; for($j=0;$j<7;$j++){ if($i>$days){ echo "<td> </td>"; } else{ echo "<td>{$i}</td>"; } $i++; } echo "</tr>"; } echo "</table>"; ?>
添加背景色
<?php header("content-type:text/html;charset=utf-8"); $days= 31; echo "<table width='700px' border='1px'>"; for($i=1;$i<=$days;){ $k++; if($k%2==1){ echo "<tr bgcolor='#cccccc'>"; }else{ echo "<tr>"; } for($j=0;$j<7;$j++){ if($i>$days){ echo "<td> </td>"; } else{ echo "<td>{$i}</td>"; } $i++; } echo "</tr>"; } echo "</table>"; ?> 中止脚本 2.exit();中止脚本使用 3.die(); <?php echo "11111<br>"; die("从这儿开始脚本中止"); echo "2222222"; ?>

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

到ImprovephPapplicationspeed,關注台詞:1)啟用opcodeCachingwithapCutoredUcescriptexecutiontime.2)實現databasequerycachingingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandreduceconnection.4 limitesclection.4.4

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

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

phpisusedforsenderemailsduetoitsbuilt-inmail()函數andsupportivelibrariesLikePhpMailerAndSwiftMailer.1)usethemail()functionForbasiceMails,butithasimails.2)butithasimail.2)

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

依賴性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增強量強制性,可驗證性和MATIALWINABIOS.ItallowSpasspassingDepentenciesLikEdenciesLikedAbaseConnectionStoclasseconnectionStoclasseSasasasasareTers,interitationAseTestingEaseTestingEaseTestingEaseTestingEasingAndScalability。

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
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver Mac版
視覺化網頁開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版
中文版,非常好用

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!