第三章 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 '循环结束';
?>

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.

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.

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: &

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.

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

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

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

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
