search
HomeBackend DevelopmentPHP Tutorialphp学习笔记(三)操作符与控制结构_PHP

一.字符串插入
为了给开发人员处理字符串值提供最大的灵活性,PHP 为字面插入和内容插入提供了
一种方法。
双引号提供了最大的灵活性,原因是变量和转移序列都会得到相应的解析。
复制代码 代码如下:
$userName = "张三";
echo "His name is $userName ";
echo "
";
//中文会出现一些问题
echo "他的名字叫$userName ,他19岁了,已经长大成人了!";
echo "
";
//可以采用字符串连接的方式解决
echo "他的名字叫".$userName.",他19岁了,已经长大成人了!"
//转义字符可以得到解析
echo "虽然他的QQ号有很多女生,\n但一个都不属于他";
?>

部分常用的转义字符
转义序列描述
\n 换行符
\r 回车
\t 水平制表图
\\ 反斜杠
\$ 美元符
\" 双引号

单引号会按照声明的原样解释,解析字符串时,变量和转义序列都不会进行解析。
echo '吴祁的变量名为:$userName,转义字符\n在单引号中无效'
?>
二.操作符
操作符是用来对数组和变量进行某种操作运算的符号。
算术操作符
复合赋值操作符
前置递增递减和后置递增递减运算符:
$a=++$b;
$a=$b++;
$a=--$b;
$a=$b--;
比较运算符
操作符名称示例
+ 加$a+$b
- 减$a-$b
* 乘$a*$b
/ 除$a/$b
% 取余$a%$b
操作符使用方法等价于
+= $a+=$b $a=$a+$b
-= $a-=$b $a=$a-$b
*= $a*=$b $a=$a*$b
/= $a/=$b $a=$a/$b
%= $a%=$b $a=$a%$b
.= $a.=$b $a=$a.$b
操作符名称使用方法

= = 等于$a= =$b
= = = 恒等$a= = =$b
!= 不等$a!=$b
!= = 不恒等$a!= =$b
不等$a$b
> 大于$a>$b
>= 大于等于$a>=$b

注:恒等表示只有两边操作数相等并且数据类型也相当才返回true;
例如:0= ="0" 这个返回为true ,因为操作数相等
0= = ="0" 这个返回为false,因为数据类型不同


逻辑运算符
! 非!$b
如果$b 是false, 则
返回true;否则相反
&& 与$a&&$b
如果$a 和$b 都是
true,则结果为true;
否则为false
|| 或$a||$b
如果$a 和$b 中有一
个为true 或者都为
true 时,其结果为
true;否则为false
and 与$a and $b
与&&相同,但其优
先级较低
or 或$a or $b
与||相同,但其优先
级较低

操作符"and"和"or"比&&和||的优先级要低。
三元操作符
Condition ? value if true : value if false
示例:($grade>=50 ? "Passed" : "Failed")


错误抑制操作符:
$a=@(57/0);
除数不能为0,会出错,所以加上@避免出现错误警告。
数组操作符
+ 联合!$b
返回一个包含了
$a 和$b 中所有元
素的数组
= = 等价$a&&$b
如果$a 和$b 具有
相同的元素,返回
true
= = = 恒等$a||$b
如果$a 和$b 具有
相同的元素以及
相同的顺序,返回
true
!= 非等价$a and $b
如果$a 和$b 不是
等价的,返回true
非等价
如果$a 和$b 不是
等价的,返回true
!= = 非恒等$a or $b
如果$a 和$b 不是
恒等的,返回true

操作符的优先级和结合性:
一般地说,操作符具有一组优先级,也就是执行他们的顺序。
操作符还具有结合性,也就是同一优先级的操作符的执行顺序。这种顺序通常有从
左到右,从右到左或者不相关。
下面给出操作符优先级的表。最上面的操作符优先级最低,按着表的由上而下的顺
序,优先级递增。

操作符优先级
左,
左Or
左Xor
左And
右Print

= += -= *= /= .= %= &= |= ^= ~= >>=
左?:
左||
左&&
左|
左^
左&
不相关= = != = = = = != =
不相关>=
左>
左+ - .
左* / %

! ~ ++ --
(int)(double)(string)(array)(object) @
右[]
不相关New
不相关()
为了避免优先级混乱,可以使用括号避开优先级。
三.控制结构
如果我们希望有效地相应用户的输入,代码就需要具有判断能力。能够让程序进行判断
的结构称为条件。
if
条件判断语句
if (条件判断){
//......
}
if (条件判断){
//....
}
else {
//....
}
if (条件判断) {
//...
}
左||
左&&
左|
左^
左&
不相关= = != = = = = != =
不相关>=
左>
左+ - .
左* / %

! ~ ++ --
(int)(double)(string)(array)(object) @
右[]
不相关New
不相关

elseif {
//...
}
elseif {
//....
}
else {
//....
}
switch
语句
switch (变量) {
case "值1":
//...
break;
case "值2":
//...
break;
case "值N":
//...
break;
default:
//....
break;
}
while
循环
while (条件) {
//....
}
for
循环
for (初始值;条件;计数器) {
//....
}
除了for 循环外,PHP 还提供了foreach 循环,它专门用于数组的使用。我们在数
组中详细介绍它。
do
while
循环
do {
//...
} while(条件);
如果希望停止一段代码的执行,根据所需要达到的效果不同,可以有3 中方法实现。

第一种:break; 退出循环; 第二种是exit ;退出程序第三种是continue;退出当前循

if (条件)
{
break; //continue
}
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
PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

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.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

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.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor