Home php教程 php手册 第三章 php操作符与控制结构代码

第三章 php操作符与控制结构代码

Jun 13, 2016 pm 12:03 PM
php one and code use the difference pair copy string quotation marks control insert operate third chapter structure

一.字符串插入
双引号与单引号的区别:
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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles