php学习笔记(三)操作符与控制结构_php入门_脚本之家
好久没更新了,这段时间挺忙的。下面继续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
}

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Usage of most Linux commands using the '!' symbol may vary in different shells. While the examples I provide are typically used in bash shells, some other Linux shells may have different implementations or may not support certain uses of the '!' symbol at all. Let’s dive into the surprising and mysterious uses of the ‘!’ symbol in Linux commands. 1. Use the command number to run a command from the history. What you may not know is that you can run a command from the command history (commands that have already been executed). First, find the number of the command by running the 'history' command. linuxmi@linuxmi:~/www.linuxmi.

The modulo equal operator (%) is a very commonly used operator in PHP and is used to calculate the remainder of the division of two numbers. In this article, we will take an in-depth look at the usage of the modular equals operator and provide specific code examples to help readers better understand. First, let's look at a simple example. Suppose we need to calculate the remainder of dividing one number by another: $a=10;$b=3;$remainder=$a%$b;echo"10 divided by 3 The remainder is: &

SQL in operator usage: 1. Single column matching, you can use the IN operator to match multiple values in a column; 2. Multi-column matching, the IN operator can also be used to match values in multiple columns; 3. Subquery, The IN operator can also be used with a subquery, which is a query statement nested within the main query.

In previous PHP versions, if we did not define a variable, using it directly would result in an Undefined variable error. However, in PHP7, we can use some new features to avoid this problem. These new features include two new operators: ?-> and ??. They can solve two different types of problems respectively.

Let us consider that in C or C++, there is a similar statement: c=a+++b; So what is the meaning of this line of code? Okay, let a and b be 2 and 5 respectively. This expression can be viewed as two different types. c=(a++)+bc=a+(++b) has post-increment operator and pre-increment operator. How they are used depends on how they are used. There are two basic concepts. Priority and associativity. Now if we check the expression from left to right, the result will be these two. c=(a++)+b→2+5=7c=a+(++b)→2+6=8 Now let’s check which option is selected by the compiler - example code #include<io

How does the new operator in js work? Specific code examples are needed. The new operator in js is a keyword used to create objects. Its function is to create a new instance object based on the specified constructor and return a reference to the object. When using the new operator, the following steps are actually performed: create a new empty object; point the prototype of the empty object to the prototype object of the constructor; assign the scope of the constructor to the new object (so this points to new object); execute the code in the constructor and give the new object

With the development of the Internet, PHP has gradually become one of the most popular programming languages in web development. However, with the rapid development of PHP, object-oriented programming has become one of the necessary skills in PHP development. In this article, we will discuss how to master object-oriented programming skills in PHP development. Understanding the Concepts of Object-Oriented Programming Object-oriented programming is a programming paradigm that organizes code and data through the use of objects (classes, properties, and methods). In object-oriented programming, code is organized into reusable modules, thereby improving the program's

PHP is a widely used programming language that supports a variety of control structures, among which loop control structures are an important one. A loop control structure repeatedly executes one or more statements in a program until a specified condition is met. In this article, we will explore loop control structures and their implementation in PHP. 1. The for loop control structure The for loop control structure is a structure used to execute statements in a loop, which can repeatedly execute a block of code a specified number of times. The syntax of the for loop is as follows: for(initiali
