search
HomeBackend DevelopmentPHP TutorialPHP operators and control structures_PHP tutorial

PHP operators and control structures_PHP tutorial

Jul 21, 2016 pm 03:20 PM
phpandvariablenameandrightcontroloperatearrayyesofsymbolarithmeticstructureOperationconduct

操作符

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

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

>

大于

$a>$b

小于等于

$a

>=

大于等于

$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 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
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools