search
HomeBackend DevelopmentPHP Tutorial第三章 php操作符与控制结构代码_PHP

一.字符串插入
双引号与单引号的区别:
1.双引号的使用:
复制代码 代码如下:

//双引号可以解析变量和转义字符
$username = "jack";
echo "his name is $username!";
echo "
";
$username = "小东";
//如果是英文的感叹号会正常解析变量
echo "他的名字是$username!";//他的名字是小东!
echo "
";
//如果是中文的感叹号则会解析不出来
echo "他的名字是$username!";//他的名字是
echo "
";
//转义字符在这里虽然被解析出来了,但是\n是在源代码里换行
//浏览器显示只是一个字符的位置
echo "他的名字是$username,\n他今年20岁了";//他的名字是小东, 他今年20岁了
echo "
";
//为了避免出现错误,推荐使用字符串连接的方式
echo "他的名字是".$username.",他今年20岁了";//他的名字是小东,他今年20岁了
?>

2.单引号的使用:
复制代码 代码如下:
//单引号只是输出字符串字面值,
//不会解析变量和转义字符。
//也不会进行语法加亮提示
$username = 'anllin';
echo 'his name is $username,\n his age is 20.';
//output
//his name is $username,\n his age is 20.
?>

部分常用的转义字符

转义序列

描述

\n

换行符

\r

回车

\t

水平制表图

\\

反斜杠

\$

美元符

\”

双引号


二.操作符
复制代码 代码如下:
//算术操作符
$a = 5;
$b = 3;
echo $a + $b;
echo '
';
echo $a - $b;
echo '
';
echo $a * $b;
echo '
';
echo $a / $b;
echo '
';
echo $a % $b;
?>

8
2
15
1.66666666667
2
复制代码 代码如下:
//复合赋值操作符
$a = 5;
$b = 3;
echo $a += $b;
echo '
';
echo $a -= $b;
echo '
';
echo $a *= $b;
echo '
';
echo $a /= $b;
echo '
';
echo $a %= $b;
echo '
';
echo $a .= $b;
?>

8
5
15
5
2
23
复制代码 代码如下:
//递增递减运算符
$a = 5;
echo ++$a;
echo '
';
echo $a++;
echo '
';
echo --$a;
echo '
';
echo $a--;
?>

6
6
6
6
复制代码 代码如下:
$a = 5;
$b = 3;
$c = 5;
$d = '5';
echo $a == $c;
echo '
';
echo $a === $c;
echo '
';
echo $a == $d;
echo '
';
echo $a != $b;
echo '
';
echo $a !== $d;
echo '
';
echo $a != $b;
echo '
';
echo $a > $b;
echo '
';
echo $b echo '
';
echo $a >= $c;
echo '
';
echo $a ?>

1
1
1
1
1
1
1
1
1
1
复制代码 代码如下:
$a = false;
echo ! $a;
echo '
';
$b = 5;
$c = 3;
echo $b > 0 && $c > 0;
echo '
';
echo $b > 0 and $c > 0;
echo '
';
echo $b != 0 || $c != 0;
echo '
';
echo $b != 0 or $c != 0;
echo '
';
?>

1
1
1
1
1
运算符”and”和”or”比&&和||的优先级要低
三元操作符
复制代码 代码如下:
$a = 100;
echo $a > 60 ? 'success':'fail';
?>

success
错误抑制操作符
复制代码 代码如下:
echo @(100/0);
?>

 

 

三.控制结构
If条件判断语句
复制代码 代码如下:

$a = 10;
if ($a > 0)
{
echo '整数大于零';
}
echo '
';
if ($a > 0)
{
echo '整数大于零';
}
else if($a {
echo '整数小于零';
}
else
{
echo '整数等于零';
}
?>

Switch语句
复制代码 代码如下:

$role = 'admin';
switch ($role)
{
case 'admin' :
echo '管理员';
break;
case 'user' :
echo '普通用户';
break;
case 'guest' :
echo '游客';
break;
default :
echo '游客';
break;
}
?>

While循环语句
复制代码 代码如下:
$a = 10;
while ( $a > 0 )
{
echo $a --;
echo '
';
}
?>

Do while 循环语句
复制代码 代码如下:
$a = 10;
do
{
echo $a --;
echo '
';
}
while ( $a > 0 )
?>

For循环语句
复制代码 代码如下:
for($a = 0; $a {
echo $a;
echo '
';
}
?>

Break语句
复制代码 代码如下:

for($a = 0; $a {
echo $a;
echo '
';
if($a ==5)
{
break;//终止循环,但执行循环后面的语句
}
}
echo '循环结束';
?>

Exit语句
复制代码 代码如下:
for($a = 0; $a {
echo $a;
echo '
';
if($a ==5)
{
exit;//直接退出,循环后面的语句不执行
}
}
echo '循环结束';
?>

Continue语句
复制代码 代码如下:
for($a = 0; $a {
echo $a;
echo '
';
if($a ==5)
{
continue;//结束本次循环,继续下次循环,循环后面的语句依然执行
}
}
echo '循环结束';
?>
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
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

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

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

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.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.