Home Backend Development PHP Tutorial PHP operators and control structures_PHP tutorial

PHP operators and control structures_PHP tutorial

Jul 21, 2016 pm 03:20 PM
php and variable name and right control operate array yes of symbol arithmetic structure Operation conduct

操作符

操作符是用来对数组和变量进行某种操作运算的符号。

1、算术操作符

操作符

名称

示例

+

$a+$b

-

$a-$b

*

$a*$b

/

$a/$b

%

取余

$a%$b

2、复合赋值操作符

操作符

使用方法

等价于

+=

$a+=$b

$a=$a+$b

-=

$a-=$b

$a=$a-$b

*=

$a*=$b

$a=$a*$b

/=

$a/=$b

$a=$a/$b

%=

$a%=$b

$a=$a%$b

.=

$a.=$b

$a=$a.$b

前置递增递减和后置递增递减运算符:

$a=++$b;

$a=$b++;

$a=--$b;

$a=$b--;

3、比较运算符

操作符

名称

使用方法

= =

等于

$a= =$b

= = =

恒等

$a= = =$b

!=

不等

$a!=$b

!= =

不恒等

$a!= =$b

<>

不等

$a<>$b

<

小于

$a<$b

>

大于

$a>$b

<=

小于等于

$a<=$b

>=

大于等于

$a>=$b

注:恒等表示只有两边操作数相等并且数据类型也相当才返回true;

例如:0= ="0" 这个返回为true ,因为操作数相等

      0= = ="0"  这个返回为false,因为数据类型不同

4、逻辑运算符

操作符

使用方法

使用方法

说明

!

!$b

如果$bfalse,则返回true;否则相反

&&

$a&&$b

如果$a$b都是true,则结果为true;否则为false

||

$a||$b

如果$a$b中有一个为true或者都为true时,其结果为true;否则为false

and

$a and $b

&&相同,但其优先级较低

or

$a or $b

||相同,但其优先级较低

操作符"and""or"&&||的优先级要低。

5、三元操作符

Condition ? value if true : value if false

示例:($grade>=50 ? "Passed" : "Failed")

6、错误抑制操作符:

$a=@(57/0);

除数不能为0,会出错,所以加上@避免出现错误警告。

7、数组操作符

操作符

使用方法

使用方法

说明

+

联合

!$b

返回一个包含了$a$b中所有元素的数组

= =

等价

$a&&$b

如果$a$b具有相同的元素,返回true

= = =

恒等

$a||$b

如果$a$b具有相同的元素以及相同的顺序,返回true

!=

非等价

$a and $b

如果$a$b不是等价的,返回true

<>

非等价

 

如果$a$b不是等价的,返回true

!= =

非恒等

$a or $b

如果$a$b不是恒等的,返回true

操作符的优先级和结合性:

一般地说,操作符具有一组优先级,也就是执行他们的顺序。

操作符还具有结合性,也就是同一优先级的操作符的执行顺序。这种顺序通常有从左到右,从右到左或者不相关。

下面给出操作符优先级的表。最上面的操作符优先级最低,按着表的由上而下的顺序,优先级递增。

操作符优先级

结合性

操作符

Or

Xor

And

Print

= += -= *= /= .= %= &= |= ^= ~= <<= >>=

:

||

&&

|

^

&

不相关

= =  != =  = = =  != =

不相关

<<= >>=

<< >>

+ - .

* / %

! ~ ++ -- (int)(double)(string)(array)(object) @

[]

不相关

New

不相关

()

To avoid priority confusion, you can use parentheses to avoid priorities.

Control structure

If we want to effectively respond to user input, the code needs to be judgmental. The structure that allows the program to make judgments is called a condition.

1. There are three structures of if..else loops
The first one only uses the if condition and treats it as a simple judgment. Interpreted as "what to do if something happens." The syntax is as follows:
if (expr) { statement }
where expr is the condition for judgment, usually using logical operation symbols as the condition for judgment. The statement is the execution part of the program that meets the conditions. If the program has only one line, the curly brackets {} can be omitted.
Example: This example omits the curly braces.

Copy code The code is as follows:

if ($state==1)echo "Haha " ;
?>

Special attention here is that the judgment of equality is == instead of =. ASP programmers may often make this mistake, = is an assignment.
Example: The execution part of this example has three lines, and the curly brackets cannot be omitted.
Copy code The code is as follows:

if ($state==1) {
echo "haha;
echo "
" ;
}
?>

The second is to add an else condition in addition to if , can be interpreted as "what to do if something happens, or how to solve it otherwise" The syntax is as follows:
if (expr) { statement1 } else { statement2 }
Example: Modify the above example to be more complete. Processing. Since there is only one line of instructions for execution, there is no need to add curly brackets
Copy the code The code is as follows:
<.>if ($state==1) {
echo "Haha" ;
echo "
";
}
else{
echo "Haha";
echo "
";
}
?>


The third type is the recursive if..else loop, usually It is used for various decision making. It combines several if..else and applies it directly:


Copy code The code is as follows:
if ( $a > $b ) {
echo "a is bigger than b" ;
} elseif ( $a = = $b ) {
echo "a is equal to b" ;
} else {
echo "a is smaller than b" ;
}
?>


The above example only uses a two-level if..else loop to compare the two variables a and b. When actually using this kind of recursive if..else loop, please use it carefully, because too many levels of loops can easily make the design Problems with the logic, or missing braces, etc., will cause inexplicable problems in the program.
2. There is only one type of for loop with no changes. Its syntax is as follows:
for ( expr1; expr2; expr3) { statement }
where expr1 is the initial value of the condition. expr2 is the condition for judgment. Logical operators are usually used as the condition for judgment. expr3 is the condition to be executed after executing the statement. The part is used to change the conditions for the next cycle to judge, such as adding one...etc. The statement is the execution part of the program that meets the conditions. If the program has only one line, the curly brackets {} can be omitted.
The following example is written using a for loop:


Copy the code The code is as follows:
for ( $i = 1 ; $i <= 10 ; $i ++) {
echo "This is the ".$i."th loop
" ;
}
?>


3. The switch loop usually handles compound conditional judgments. Each sub-condition is part of the case instruction. In practice, if many similar if instructions are used, they can be synthesized into a switch loop.
The syntax is as follows:
switch (expr) { case expr1: statement1; break; case expr2: statement2; break; default: statementN; break; }
The expr condition is usually a variable name. The exprN after case usually represents the variable value. After the colon is the part to be executed that meets the condition. Be sure to use break to break out of the loop.


Copy code The code is as follows:
switch ( date ( "D" )) {
case "Mon" :
echo "Today is Monday" ;
break;
case "Tue" :
echo "Today is Tuesday" ;
break;
case "Wed " :
echo "Today is Wednesday" ;
break;
case "Thu" :
echo "Today is Thursday" ;
break;
case "Fri" :
echo "Today is Friday" ;
break;
default:
echo "Today is a holiday" ;
break;
}
?>


What needs to be noted here is break; don’t omit it, default, omission is okay.
Obviously, using an if loop in the above example is very troublesome. Of course, when designing, the conditions with the greatest probability of occurrence should be placed at the front and the conditions with the least occurrence at the end, which can increase the execution efficiency of the program. In the above example, since the probability of occurrence is the same every day, there is no need to pay attention to the order of the conditions.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325064.htmlTechArticleOperator Operator is a symbol used to perform certain operations on arrays and variables. 1. Arithmetic operator operator name example + Add $a+$b - Subtract $a-$b * Multiply $a*$b / Divide $a/$b % Remainder...
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 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles