PHP的基本语法(三)
八、条件语句
1、if 语句
如果指定条件为真,则执行代码
2、if…else 语句
如果条件为 true,则执行代码;如果条件为 false,则执行另一端代码
3、if…elseif….else 语句
选择若干段代码块之一来执行
<code class=" hljs php"><span class="hljs-variable">$num</span>=<span class="hljs-number">3</span>;<span class="hljs-keyword">if</span>(<span class="hljs-variable">$num</span>><span class="hljs-number">3</span>)<span class="hljs-keyword">echo</span> <span class="hljs-string">"\$num>1"</span>;<span class="hljs-keyword">elseif</span>(<span class="hljs-variable">$num</span>==<span class="hljs-number">3</span>)<span class="hljs-keyword">echo</span> <span class="hljs-string">"\$num=3"</span>;<span class="hljs-keyword">else</span><span class="hljs-keyword">echo</span> <span class="hljs-string">"\$num<3"</span>;</code>
运行结果:$num=3
4、switch 语句
有选择地执行若干代码块之一
工作原理:
- 对表达式(通常是变量)进行一次计算
- 把表达式的值与结构中 case 的值进行比较
- 如果存在匹配,则执行与 case 关联的代码
- 代码执行后,break 语句阻止代码跳入下一个 case 中继续执行
- 如果没有 case 为真,则使用 default 语句
<code class=" hljs php"><span class="hljs-variable">$x</span>=<span class="hljs-number">4</span>;<span class="hljs-keyword">switch</span> (<span class="hljs-variable">$x</span>){<span class="hljs-keyword">case</span> <span class="hljs-number">1</span>: <span class="hljs-keyword">echo</span> <span class="hljs-string">"Number 1"</span>; <span class="hljs-keyword">break</span>;<span class="hljs-keyword">case</span> <span class="hljs-number">2</span>: <span class="hljs-keyword">echo</span> <span class="hljs-string">"Number 2"</span>; <span class="hljs-keyword">break</span>;<span class="hljs-keyword">default</span>: <span class="hljs-keyword">echo</span> <span class="hljs-string">"No number between 1 and 2"</span>;}</code>
运行结果:No number between 1 and 2
九、循环语句
1、while
只要指定条件为真,则循环代码块
2、do…while
先执行一次代码块,然后只要指定条件为真则重复循环
3、for
循环代码块指定次数
4、foreach
遍历数组中的每个元素并循环代码块
<code class=" hljs handlebars"><span class="xml"><span class="hljs-tag"><span class="php"><span class="hljs-preprocessor"><?PHP</span><span class="hljs-variable">$its</span> = <span class="hljs-keyword">array</span>(<span class="hljs-string">"Apple"</span>,<span class="hljs-string">"Google"</span>,<span class="hljs-string">"Microsoft"</span>,<span class="hljs-string">"Solidot"</span>); <span class="hljs-keyword">foreach</span> (<span class="hljs-variable">$its</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$it</span>) { <span class="hljs-keyword">echo</span> <span class="hljs-string">"$it <br>"</span>;}<span class="hljs-preprocessor">?></span></span></span></span></code>
运行结果: Apple <br>Google <br>Microsoft <br>Solidot
十、函数
1、函数的类型
函数是可以在程序中重复使用的语句块。
页面加载时函数不会立即执行。
函数只有在被调用时才会执行。
- 内建函数:超过一千个内建函数
- 自定义函数:以“function”关键字开头;函数名能够以字母或下划线开头(而非数字),对大小写不敏感,应该能够反映函数所执行的任务。
2、函数的几个要素
- PHP函数参数:参数被定义在函数名之后,括号内部。您可以添加任意多参数,只要用逗号隔开即可。
- PHP函数默认参数:直接在参数后面赋值,当我们调用不含参数的函数时会自动赋予默认值。
- PHP函数返回值:使用return语句返回值。
<code class=" hljs php"><span class="hljs-preprocessor"><?php</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">passwd</span><span class="hljs-params">(<span class="hljs-variable">$account</span>,<span class="hljs-variable">$password</span>=<span class="hljs-number">123456</span>)</span>{</span><span class="hljs-keyword">return</span> <span class="hljs-variable">$password</span>;}<span class="hljs-variable">$pLee</span> = passwd(<span class="hljs-string">"Lee"</span>);<span class="hljs-keyword">echo</span> <span class="hljs-string">"$pLee"</span>;<span class="hljs-preprocessor">?></span></code>
运行结果:123456
版权声明:本文为Lshare原创文章,需要转载的请联系我,有问题欢迎评论或私信。

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

TooptimizePHPapplicationsforperformance,usecaching,databaseoptimization,opcodecaching,andserverconfiguration.1)ImplementcachingwithAPCutoreducedatafetchtimes.2)Optimizedatabasesbyindexing,balancingreadandwriteoperations.3)EnableOPcachetoavoidrecompil

DependencyinjectioninPHPisadesignpatternthatenhancesflexibility,testability,andmaintainabilitybyprovidingexternaldependenciestoclasses.Itallowsforloosecoupling,easiertestingthroughmocking,andmodulardesign,butrequirescarefulstructuringtoavoidover-inje

PHP performance optimization can be achieved through the following steps: 1) use require_once or include_once on the top of the script to reduce the number of file loads; 2) use preprocessing statements and batch processing to reduce the number of database queries; 3) configure OPcache for opcode cache; 4) enable and configure PHP-FPM optimization process management; 5) use CDN to distribute static resources; 6) use Xdebug or Blackfire for code performance analysis; 7) select efficient data structures such as arrays; 8) write modular code for optimization execution.

OpcodecachingsignificantlyimprovesPHPperformancebycachingcompiledcode,reducingserverloadandresponsetimes.1)ItstorescompiledPHPcodeinmemory,bypassingparsingandcompiling.2)UseOPcachebysettingparametersinphp.ini,likememoryconsumptionandscriptlimits.3)Ad


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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

WebStorm Mac version
Useful JavaScript development tools
