如何在十天内学会php之第二天
学习目的:掌握php的流程控制
1、if..else 循环有三种结构
第一种是只有用到 if 条件,当作单纯的判断。解释成 "若发生了某事则怎样处理"。语法如下:
if (expr) { statement }
其中的 expr 为判断的条件,通常都是用逻辑运算符号当判断的条件。而 statement 为符合条件的执行部分程序,若程序只有一行,可以省略大括号 {}。
范例:本例省略大括号。
<?php
if ($state==1)echo "哈哈" ;
?>
这里特别注意的是,判断是否相等是==而不是=,ASP程序员可能常犯这个错误,= 是赋值。
范例:本例的执行部分有三行,不可省略大括号。
<?php
if ($state==1) {
echo "哈哈 ;
echo "<br>" ;
}
?>
两种是除了 if 之外,加上了 else 的条件,可解释成 "若发生了某事则怎样处理,否则该如何解决"。语法如下
if (expr) { statement1 } else { statement2 } 范例:上面的例子来修改成更完整的处理。其中的 else 由于只有一行执行的指令,因此不用加上大括号。
<?php
if ($state==1) {
echo "哈哈" ;
echo "<br>";
}
else{
echo "呵呵";
echo "<br>";
}
?>
第三种就是递归的 if..else 循环,通常用在多种决策判断时。它将数个 if..else 拿来合并运用处理。
直接看下面的例子
<?php
if ( $a > $b ) {
echo "a 比 b 大" ;
} elseif ( $a == $b ) {
echo "a 等于 b" ;
} else {
echo "a 比 b 小" ;
}
?>
上例只用二层的 if..else 循环,用来比较 a 和 b 两个变量。实际要使用这种递归 if..else 循环时,请小心使用,因为太多层的循环容易使设计的逻辑出问题,或者少打了大括号等,都会造成程序出现莫名其妙的问题。
2、 for 循环就单纯只有一种,没有变化,它的语法如下
for (expr1; expr2; expr3) { statement }
其中的 expr1 为条件的初始值。expr2 为判断的条件,通常都是用逻辑运算符号 (logical operators) 当判断的条件。expr3 为执行 statement 后要执行的部份,用来改变条件,供下次的循环判断,如加一..等等。而 statement 为符合条件的执行部分程序,若程序只有一行,可以省略大括号 {}。
下例是用 for 循环写的的例子。
<?php
for ( $i = 1 ; $i <= 10 ; $i ++) {
echo "这是第".$i."次循环<br>" ;
}
?>
3、 switch 循环,通常处理复合式的条件判断,每个子条件,都是 case 指令部分。在实作上若使用许多类似的 if 指令,可以将它综合成 switch 循环。
语法如下
switch (expr) { case expr1: statement1; break; case expr2: statement2; break; default: statementN; break; }
其中的 expr 条件,通常为变量名称。而 case 后的 exprN,通常表示变量值。冒号后则为符合该条件要执行的部分。注意要用 break 跳离循环
<?php
switch ( date ( "D" )) {
case "Mon" :
echo "今天星期一" ;
break;
case "Tue" :
echo "今天星期二" ;
break;
case "Wed" :
echo "今天星期三" ;
break;
case "Thu" :
echo "今天星期四" ;
break;
case "Fri" :
echo "今天星期五" ;
break;
default:
echo "今天放假" ;
break;
}
?>
这里需要注意的是break;别遗漏了,default,省略是可以的。
上述的例子用 if 循环就很麻烦了。当然在设计时,要将出现机率最大的条件放在最前面,最少出现的条件放在最后面,可以增加程序的执行效率。上例由于每天出现的机率相同,所以不用注意条件的顺序。
今天就说到这里,明天开始说数据库的使用。

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



In today's era of rapid technological development, programming languages are springing up like mushrooms after a rain. One of the languages that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

"Go Language Development Essentials: 5 Popular Framework Recommendations" As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

With the development of the Internet and the advancement of information technology, the era of big data has arrived, and fields such as data analysis and machine learning have also been widely used. In these fields, task scheduling is an inevitable problem. How to achieve efficient task scheduling is crucial to improving efficiency. In this article, we will introduce how to use Golang's web framework Echo framework to implement distributed task scheduling. 1. Introduction to the Echo framework Echo is a high-performance, scalable, lightweight GoWeb framework. It is based on HTTP

Laravel is a popular PHP framework that is highly scalable and efficient. It provides many powerful tools and libraries that allow developers to quickly build high-quality web applications. Among them, LaravelEcho and Pusher are two very important tools through which WebSockets communication can be easily implemented. This article will detail how to use these two tools in Laravel applications. What are WebSockets? WebSockets

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

Detailed explanation of the role and usage of the echo keyword in PHP PHP is a widely used server-side scripting language, which is widely used in web development. The echo keyword is a method used to output content in PHP. This article will introduce in detail the function and use of the echo keyword. Function: The main function of the echo keyword is to output content to the browser. In web development, we need to dynamically present data to the front-end page. At this time, we can use the echo keyword to output the data to the page. e

As a fast and efficient programming language, Go language has always been favored by programmers. In the Go language ecosystem, frameworks play a vital role in helping developers build applications faster. This article will introduce five Go language frameworks to let you understand their characteristics and usage. 1. Gin framework The Gin framework is a lightweight Web framework with fast and high performance characteristics. Use the Gin framework to quickly build RESTful APIs and web applications. Here is a simple example code:

The most popular Go frameworks at present are: Gin: lightweight, high-performance web framework, simple and easy to use. Echo: A fast, highly customizable web framework that provides high-performance routing and middleware. GorillaMux: A fast and flexible multiplexer that provides advanced routing configuration options. Fiber: A performance-optimized, high-performance web framework that handles high concurrent requests. Martini: A modular web framework with object-oriented design that provides a rich feature set.
