PHP语句
当操作数和操作符组合到一起,即构成表达式.
这些就是表达式:
12 这是最基本的表达式
-12
-12+5
-12+5*(24/ 3)
而语句则由表达式组成。简单的、复杂的语句构成了程序。
这些就是语句:
; 这是最基本的语句
$name='John';
PHP规定每个语句结束时都要加上分号";"
PHP共有六种类型语句,见表
语句类型 |
描 述 |
非执行语句 |
需计算但不执行动作 |
执行语句 |
执行某一动作 |
赋值语句 |
给变量赋值 |
判断语句 |
判断条件,并决定执行哪个动作 |
循环语句 |
重复执行一系列语句直到某条件为真或直到某条件为假时为止 |
跳转语句 |
无条件改变程序流程到程序中的另一行继续执行 |
1、非执行语句、执行语句、赋值语句相对简单,略
2、判断语句
if...else...语句
第一种:如果表达式成立,则执行需要执行的语句
if (表达式)
{
需要执行的语句
}
第二种:如果表达式成立,则执行需要执行的语句1,否则执行需要执行的语句2
if (表达式)
{
需要执行的语句1
}
else
{
需要执行的语句2
}
第三种:如果表达式1成立,则执行需要执行的语句1,
否则判断表达式2,若成立,则执行需要执行的语句2
若还不成立,则执行需要执行的语句3
if (表达式1)
{
需要执行的语句1
}
elseif (表达式2)
{
需要执行的语句2
}
else
{
需要执行的语句3
}
switch 语句
上面的if语句判断一、两个条件值时,处理起来还比较方便。但如果需要同时测试、判断多个条件值时,if语句处理起来就比较烦了。所以有了它--switch 语句。
switch (VARIABLE) {
case VALUE1:
break;
case VALUE2:
break;
case VALUE3:
break;
case VALUEn:
break;
default:
break;
执行时,PHP从上往下检查变量:
如果变量等于某个case语句中的值,
那就紧接着寻找break关键字,
如果这个case语句中没有break关键字,
则执行下一个case语句,
直到找到break关键字为止。
如果所有case语句中的值都不等于变量,
则执行default语句。
例:
switch($str_input){
case 'print':
case 'echo':
echo"do something"
break;
case 1:
case 2:
echo"do another"
break;
default:
echo"nothing!"
break;
}
3、循环语句:重复执行一系列语句直到某条件为真或直到某条件为假时为止
for (...;...;...){......}语句
for 语句由三个表达式和一段语句组成
即:for (初始化语句;条件表达式;相应操作语句){要执行的语句}
例:
for ($loop_variable=0;$loop_variable{
echo"Look this:loop_variable=$loop_variablebr>";
}
则显示:
Look this:loop_variable=0
Look this:loop_variable=1
...
Look this:loop_variable=98
Look this:loop_variable=99
do 、while 语句
当条件为真时,while 循环重复一段语句块
1、while (条件)
{
要执行的语句
}
2、while (条件):
要执行的语句;
Endwhile;
3、do {
要执行的语句
}(条件);
4、do {
要执行的语句
} while(条件);
4、跳转语句
这里不像BASIC语言一样有一个“go”关键字。
而是break语句和 continue语句。
break使PHP停止执行当前的语句块,从紧跟着当前语句块的语句开始运行。
例:用break语句从循环中退出。
php
for ($index=0;$index {
if ($index==3)
{
break;
}
echo"$indexbr>";
}
echo"After the loop: index=$indexbr>";
?>
显示:
0
1
2
After the loop:index=3
contiune终止循环的当前重复,并立即开始下一个重复
例:contiune在for语句中的运用。
php
for ($index=0;$index {
if ($index==3)
{
continue;
}
echo"$indexbr>";
}
?>
显示:
0
1
2 3给漏掉了,没显示出来
4

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

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

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











index.html represents the home page file of the web page and is the default page of the website. When a user visits a website, the index.html page is usually loaded first. HTML (HypertextMarkupLanguage) is a markup language used to create web pages, and index.html is also an HTML file. It contains the structure and content of a web page, as well as tags and elements used for formatting and layout. Here is an example index.html code: <

When the HMD Skyline(available on Amazon for $499) was launched last month, it was released in two colors - Neon Pink and Twisted Black. They are now joined by a third color dubbed Blue Topaz. HMD Global has also announced an official case for the ph

Microsoft Loop, enhanced with its new feature Copilot, is a modern tool designed to improve the way teams collaborate. It consists of three main parts: components, pages, and workspaces. Components are things like lists or notes that stay updated no matter where you use them, whether in email, documents, or chat. This means you are always working with the latest information. Cycle pages are like digital whiteboards where you put all your components, tasks, and data together. These pages can grow as your project grows, making it easy to keep everything in one place. Workspaces in Loop are shared areas where your team can view and organize everything important to the project, helping everyone

PHP source code running problem: Index error resolution requires specific code examples. PHP is a widely used server-side scripting language that is often used to develop dynamic websites and web applications. However, sometimes you will encounter various problems when running PHP source code, among which "index error" is a common situation. This article will introduce some common causes and solutions of index errors, and provide specific code examples to help readers better deal with such problems. Problem Description: When running a PHP program

For PHP developers, encountering the "Undefinedvariable" warning message is a very common thing. This situation usually occurs when trying to use undefined variables, which will cause the PHP script to fail to execute normally. This article will introduce how to solve the problem of "PHPNotice:Undefinedvariable: in solution". Cause of the problem: In a PHP script, if a variable is not initialized or assigned a value, "Un

Switchcase requires specific code examples to determine variables. In programming, we often need to perform different operations based on different variable values. The switchcase statement is a convenient structure that allows you to select different blocks of code for execution based on the value of a variable. The following is a specific code example that shows how to use the switchcase statement to determine different values of variables: #includeintmain(){

The index in MySQL means index. It is a data structure used to speed up the query of database tables. The index can be compared to the catalog of a book. It stores the values of specific columns in the table and the corresponding row positions, making the database more efficient. Locate and access data quickly. The function of the index is to improve query efficiency. Without an index, the database needs to scan the entire table row by row to find matching data. This method will be very time-consuming in large tables. With an index, the database can The required data rows are quickly located in the order, which greatly improves the query speed.

Microsoft is ready to give users access to the first preview version of the Loop project. Now, let's learn where to get it, how to install it and how to get the most out of it. Want to learn how to use this software across Office apps and manage tasks? You've come to the right place. What is the Microsoft Cycle? What should we say? You can compare loops to project boards. Here you can see a list of all Loop components and Loop pages, and who is currently working on them. Think of it as a modern file explorer where everything is live and collaborative. The loop page is a separate canvas where people can share and collaborate on loop components. In addition, Loop components are constantly updated and edited, without
