PHP和MySQL入门(6)
这样的循环可以用来处理一个事物的较长的列表(例如存储在数据库中的笑话),下面我们举了一个简单的例子:计数到10。
$count = 1; while ($count echo( "$count " ); $count++; } |
我知道这段语句可能看上去挺恐怖,但是我们可以一行行地来看它。第一行定义了一个 叫$count的变量并将其赋值为1。第二行是while循环的开始,条 件是$count小于或等于(
现在我们可以看看这段程序是如何执行的了。当第一次检查条件时,$count的值是1,所以条 件为真。$count的值(1)被输出,接着$count被赋予了一个新值2。当第二次检查条件时条件仍为真,所以2 被输出,并赋了一个新值3 。这个过程被继续下去,输出了3 ,4 ,5 ,6 ,7 ,8 ,9 直到10 。最后,$count被赋予值11,条件为假,循环结束。最终的结果是输出了这样一个字符串“1 2 3 4 5 6 7 8 9 10”。
在这个例子的条件中我们使用了一个新的运算符:=(大于或等于),(大于)和!=(不等于)。最后一个也可以使用于字符串比较中。
多用途的页面
如果你想在你建立的网站的每一个网页的顶端都显示访问者的姓名。使用我们前面的自动显示欢迎信息的例子,我们已经基本上成功了一半。现在我们只要对我们示例解决这几个问题就行了:
我们需要在站点的每一个页面都显示,而不是仅仅在一个页面上。
我们无法控制在我们的站点上那一个页面会首先显示。
第一个问题的解决不是太困难的。当我们在一个页面上获得了用户名这个变量后,我们可以在任何请求中通过将其添加到一个查询字符串来传递这个变量:
A link |
请注意我们在HTML标识符的中间嵌入了PHP的代码。实际上这是非常常用的。我们已经对 echo这个函数很熟悉,但是我们还不熟悉urlencode函 数。这个函数的功能是将字符串中一些特殊的字符(例如空格)转换成特定的编码,以使它们能够在查询字符串中显示。例如,如果$name变量的值 是"Kevin Yank",其中的空格在查询字符串中不允许存在,urlencode的输出将是Kevin+Yank,在newpage.php中建立$name时,值 将会被自动转换回来。
Ok,现在已经可以将用户名传递给我们站点的第一个连接了。现在我们所需要的就是在其第一次出现时,要能获得它的值。在我们上面的例子中,我们已经做了一个HTML页面用来处理获得用户名的表单。而问题是我们不能强迫用户在每一次访问我们的站点时都从这个页面开始。

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



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...
