Table of Contents
目录
条件语句
if语句
switch语句
循环语句
while循环
do…while循环
for循环
foreach循环
Home Backend Development PHP Tutorial 前端学PHP之话语

前端学PHP之话语

Jun 13, 2016 pm 12:28 PM
echo foreach gt while

前端学PHP之语句

目录
[1]条件语句 if语句 switch语句 [2]循环语句 while循环 do…while循环 for循环 foreach循环

条件语句

  用于基于不同条件执行不同的动作

if语句

<span style="color: #0000ff;">if</span><span style="color: #000000;"> (条件) {  当条件为 </span><span style="color: #0000ff;">true</span><span style="color: #000000;"> 时执行的代码;}</span>
Copy after login
<span style="color: #0000ff;">if</span><span style="color: #000000;"> (条件) {  条件为 </span><span style="color: #0000ff;">true</span><span style="color: #000000;"> 时执行的代码;} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {  条件为 </span><span style="color: #0000ff;">false</span><span style="color: #000000;"> 时执行的代码;}</span>
Copy after login
<span style="color: #0000ff;">if</span><span style="color: #000000;"> (条件) {  条件为 </span><span style="color: #0000ff;">true</span><span style="color: #000000;"> 时执行的代码;} elseif (条件) {  条件为 </span><span style="color: #0000ff;">true</span><span style="color: #000000;"> 时执行的代码;} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {  条件为 </span><span style="color: #0000ff;">false</span><span style="color: #000000;"> 时执行的代码;}</span>
Copy after login
<span style="color: #000000;">php</span><span style="color: #800080;">$t</span>=<span style="color: #008080;">date</span>("H"<span style="color: #000000;">);</span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$t</span>) {  <span style="color: #0000ff;">echo</span> "Have a good morning!"<span style="color: #000000;">;} </span><span style="color: #0000ff;">elseif</span> (<span style="color: #800080;">$t</span>) {  <span style="color: #0000ff;">echo</span> "Have a good day!"<span style="color: #000000;">;} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {  </span><span style="color: #0000ff;">echo</span> "Have a good night!"<span style="color: #000000;">;}</span>?>
Copy after login

 

switch语句

<span style="color: #0000ff;">switch</span><span style="color: #000000;"> (expression){</span><span style="color: #0000ff;">case</span> label1:<span style="color: #000000;">  code to be executed </span><span style="color: #0000ff;">if</span> expression =<span style="color: #000000;"> label1;  </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;  </span><span style="color: #0000ff;">case</span> label2:<span style="color: #000000;">  code to be executed </span><span style="color: #0000ff;">if</span> expression =<span style="color: #000000;"> label2;  </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;</span><span style="color: #0000ff;">default</span>:<span style="color: #000000;">  code to be executed  </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> expression is different   from both label1 and label2;}</span>
Copy after login
<span style="color: #000000;">php</span><span style="color: #0000ff;">switch</span> (<span style="color: #800080;">$x</span><span style="color: #000000;">){</span><span style="color: #0000ff;">case</span> 1:  <span style="color: #0000ff;">echo</span> "Number 1"<span style="color: #000000;">;  </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;</span><span style="color: #0000ff;">case</span> 2:  <span style="color: #0000ff;">echo</span> "Number 2"<span style="color: #000000;">;  </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;</span><span style="color: #0000ff;">case</span> 3:  <span style="color: #0000ff;">echo</span> "Number 3"<span style="color: #000000;">;  </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;</span><span style="color: #0000ff;">default</span>:  <span style="color: #0000ff;">echo</span> "No number between 1 and 3"<span style="color: #000000;">;}</span>?>
Copy after login

 

循环语句

  在编写代码时,经常需要反复运行同一代码块,可以使用循环来执行这样的任务

while循环

<span style="color: #0000ff;">while</span><span style="color: #000000;"> (条件为真) {  要执行的代码;}</span>
Copy after login
<span style="color: #000000;">php</span><span style="color: #800080;">$sum</span> = 12;<span style="color: #008000;">//</span><span style="color: #008000;">小宠物当前的饥饿程度</span><span style="color: #0000ff;">echo</span> "我饿啦:-("<span style="color: #000000;">;</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;</span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$sum</span>){<span style="color: #008000;">//</span><span style="color: #008000;">小宠物的饥饿程度到100,表示小宠物吃饱啦,不用继续喂了,没吃饱继续喂食</span>    <span style="color: #800080;">$num</span> = <span style="color: #008080;">rand</span>(1,20);<span style="color: #008000;">//</span><span style="color: #008000;">随机数,模拟喂食小宠物的小面包</span>  <span style="color: #800080;">$sum</span> = <span style="color: #800080;">$sum</span> + <span style="color: #800080;">$num</span>; <span style="color: #008000;">//</span><span style="color: #008000;">小宠物吃小面包</span>  <span style="color: #0000ff;">echo</span> "我还没吃饱呢!"<span style="color: #000000;">;  </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;}</span><span style="color: #0000ff;">echo</span> "终于吃饱啦^_^"<span style="color: #000000;">;</span>?>
Copy after login

 

do…while循环

  循环首先会执行一次代码块,然后检查条件,如果指定条件为真,则重复循环

<span style="color: #0000ff;">do</span><span style="color: #000000;"> {  要执行的代码;} </span><span style="color: #0000ff;">while</span> (条件为真);
Copy after login
<span style="color: #000000;">php  </span><span style="color: #800080;">$sum</span>  = 0<span style="color: #000000;">;   </span><span style="color: #0000ff;">do</span><span style="color: #000000;">{    </span><span style="color: #800080;">$num</span> = <span style="color: #008080;">rand</span>(1,6);<span style="color: #008000;">//</span><span style="color: #008000;">获取1至6的随机数,模拟掷骰子</span>    <span style="color: #800080;">$sum</span> = <span style="color: #800080;">$sum</span>  + <span style="color: #800080;">$num</span>;<span style="color: #008000;">//</span><span style="color: #008000;">前进步长</span>  }<span style="color: #0000ff;">while</span>(<span style="color: #800080;">$num</span>==6<span style="color: #000000;">);  </span><span style="color: #0000ff;">echo</span> "do...while例子执行完毕,前进:".<span style="color: #800080;">$sum</span> ."<br>"<span style="color: #000000;">;</span>?>
Copy after login

 

for循环

  for循环语句中,初始化在循环开始前无条件求值一次,循环条件在每次循环开始前求值。如果值为true,则继续循环,执行循环体语句;如果值为false,则终止循环。递增语句在每次循环后执行

<span style="color: #0000ff;">for</span><span style="color: #000000;"> (init counter; test counter; increment counter) {  code to be executed;}    </span>
Copy after login
<span style="color: #000000;">php </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$x</span>=0; <span style="color: #800080;">$x</span>$x++<span style="color: #000000;">) {  </span><span style="color: #0000ff;">echo</span> "数字是:<span style="color: #800080;">$x</span> <br>"<span style="color: #000000;">;} </span>?>
Copy after login

 

foreach循环

  foreach循环只适用于数组,用于遍历数组中每个键/值对。每进行一次循环迭代,当前数组元素的值就会被赋值给$value变量,并且数组指针会逐一移动,直到到达最后一个数组元素。一般有两种方式:不取下标、取下标

[1]只取值,不取下标

<span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$array</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$value</span><span style="color: #000000;">) {  code to be executed;}    </span>
Copy after login
<span style="color: #000000;">php </span><span style="color: #800080;">$colors</span> = <span style="color: #0000ff;">array</span>("red","green","blue","yellow"<span style="color: #000000;">); </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$colors</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$value</span><span style="color: #000000;">) {  </span><span style="color: #0000ff;">echo</span> "<span style="color: #800080;">$value</span> <br>"<span style="color: #000000;">;}</span>?>
Copy after login

[2]同时取下标和值

<span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$array</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$index</span> => <span style="color: #800080;">$value</span><span style="color: #000000;">) {  code to be executed;} </span>
Copy after login
<span style="color: #000000;">php </span><span style="color: #800080;">$colors</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(  </span>"r"=>"red",  "g"=>"green",  "b"=>"blue",  "y"=>"yellow"<span style="color: #000000;">); </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$colors</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$value</span><span style="color: #000000;">) {  </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$key</span>.":".<span style="color: #800080;">$value</span>."<br>"<span style="color: #000000;">;}</span>?>
Copy after login

 

1楼文昊学PHP
PHP是楼主接触的第一个编程语言?
Re: 小火柴的蓝色理想
@文昊学PHP,我学前端的,主要是js,PHP了解即可
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)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

In C language, what is the difference between while(1) and while(0)? In C language, what is the difference between while(1) and while(0)? Aug 31, 2023 am 10:45 AM

We know that in C language, 'while' keyword is used to define a loop that works based on the condition passed to the loop. Now, since the condition can have two values, true or false, the code inside the while block will be executed repeatedly if the condition is true and will not be executed if the condition is false. Now, by passing parameters to the while loop, we can differentiate between while(1) and while(0) because while(1) is a loop where the condition is always considered true and hence the code inside the block will start executing repeatedly. Furthermore, we can state that it is not 1 that is passed to the loop that makes the condition true, but if any non-zero integer is passed to the while loop, then it will be considered as the true condition, so

What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? Apr 27, 2023 pm 03:40 PM

1. The difference between Iterator and foreach is the polymorphic difference (the bottom layer of foreach is Iterator) Iterator is an interface type, it does not care about the type of collection or array; both for and foreach need to know the type of collection first, even the type of elements in the collection; 1. Why is it said that the bottom layer of foreach is the code written by Iterator: Decompiled code: 2. The difference between remove in foreach and iterator. First, look at the Alibaba Java Development Manual, but no error will be reported in case 1, and an error will be reported in case 2 (java. util.ConcurrentModificationException) first

How to determine the number of foreach loop in php How to determine the number of foreach loop in php Jul 10, 2023 pm 02:18 PM

​The steps for PHP to determine the number of the foreach loop: 1. Create an array of "$fruits"; 2. Create a counter variable "$counter" with an initial value of 0; 3. Use "foreach" to loop through the array, and Increase the value of the counter variable in the loop body, and then output each element and their index; 4. Output the value of the counter variable outside the "foreach" loop to confirm which element the loop reaches.

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

PHP returns an array with key values ​​flipped PHP returns an array with key values ​​flipped Mar 21, 2024 pm 02:10 PM

This article will explain in detail how PHP returns an array after key value flipping. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Key Value Flip Array Key value flip is an operation on an array that swaps the keys and values ​​in the array to generate a new array with the original key as the value and the original value as the key. Implementation method In PHP, you can perform key-value flipping of an array through the following methods: array_flip() function: The array_flip() function is specially used for key-value flipping operations. It receives an array as argument and returns a new array with the keys and values ​​swapped. $original_array=[

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

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 Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

&quot;Go Language Development Essentials: 5 Popular Framework Recommendations&quot; 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

See all articles