Introduction to various operators in php and examples
An
operator is something that takes one or more given values (an expression, in programming jargon) and produces another value (thus the entire structure becomes an expression).
1. Arithmetic operators
Operator Name Result
$a + $b Addition Sum of $a and $b
$a - $b Subtraction The difference between $a and $b
$a * $b Multiplication The product of $a and $b
$a / $b Division The quotient of $a divided by $b
$a % $b Modulo The remainder of dividing $a by $b
2. Increment/Decrease Operator
Operator Name Result
++$a Before Add the value of $a plus one, and then perform the operation
$a++ Then add the value of $a first, perform the operation first, then add one
--$a Subtract the value of $a before subtraction by one, and then perform the operation
$a-- The value of $a is operated first, then decremented by one
Example:
<?php echo $a=5+”5th”; //输出:10 echo 10%3; //输出:1 echo 10+ $a++; //输出:20 echo 5- --$a; //输出:-5 ?>
3. Comparison operator
Operator Name Result
$a == $b is equal to TRUE if $a is equal to $b
$a === $b is congruent TRUE if $a is equal to $b and they are of the same type
$a != $b No etc. TRUE if $a is not equal to $b
$a $b is not equal TRUE if $a is not equal to $b
$a !== $b is not congruent TRUE if $a Not equal to $b, or they are of different types
$a $a > $b is greater than TRUE if $a is strictly $b
$a $a >= $b Greater than or equal to TRUE if $a is greater than or equal to $b
Another conditional operator is " ? : " (or ternary) operator.
Example:
<?php var_dump(0=="a"); //输出:bool(true) var_dump(0=="00"); //输出:bool(true) var_dump(0==="00"); //输出:bool(false) var_dump(0<>"abc"); //输出:bool(false) var_dump(0!=="01"); //输出:bool(true) $a=10; $b=20; $str=$a>$b? "true":"false"; echo $str; //输出:false ?>
4. 逻辑运算符
运算符 名称 结果
$a and $b 逻辑与 TRUE,如果 $a 与 $b 都为 TRUE。
$a or $b 逻辑或 TRUE,如果 $a 或 $b 任一为TRUE。
$a xor $b 异或 TRUE,如果 $a 和 $b 不同时
! $a 逻辑非 TRUE,如果 $a 不为 TRUE。
$a && $b 逻辑与 TRUE,如果 $a 与 $b 都为TRUE。
$a || $b 逻辑或 TRUE,如果 $a 或 $b 任一为TRUE。
其中and与&& 、or与||是同一逻辑运算符的两种写法。
逻辑与和逻辑或 都是短路运算符。
在遇到下列逻辑表达式时,PHP解释程序将不会计算右边的表达式:
<?php $a=10; if(false && (++$a)); echo $a; //输出:10 $b=10; if(true or (++$b)); echo $b; //输出:10 ?>
5. 位运算符
位运算符允许对整型数中指定的位进行置位。如果左右参数都是字符串,则位运算符将操作字符的 ASCII 值。
表达式 名称 结果
$a & $b 按位与 将把 $a 和 $b 中都为 1 的位设为 1。
$a | $b 按位或 将把 $a 或者 $b 中为 1 的位设为 1。
$a ^ $b 按位异或 将把 $a 和 $b 中不同的位设为 1。
~ $a 按位非 将 $a 中为 0 的位设为 1,反之亦然。
$a $a >> $b 右移 将 $a 中的位向右移动 $b 次(每一次 移动都表示“除以 2”)。
其他运算符
1. 字符串运算符
有两个字符串运算符。第一个是连接运算符(“.”),它返回其左右参数连接后的字符串。第二个是连接赋值运算符(“.=”),它将右边参数附加到左边的参数后。
错误抑制操作符
在最常见的数据库连接与文件创建操作或出现除0等异常时,可以用@符号来抑制函数错误信息输出到浏览器端 $a=@(5/0)
外部命令执行
使用``来运行外部系统命令,注意不是单引号,是ESC下面那个按键
<?php $out=`dir c:`; print_r($out); ?> //不建议使用
实例:
<?php $a="hello"; $a.=" world! "; //等同于:$a=$a." world!"; echo $a; //输出:hello world! $m = 3; $m += 5; //等同于:$m=$m+5; echo $m; //输出:8 $c = ($b = 4) + 5; echo $c; //输出:9 ?>
2. 运算符优先级
下表从低到高列出了运算符的优先级。
结合方向 运算符
左 ,
左 or
左 xor
左 and
右 print
右 = += -= *= /= .= %= &= |= ^= ~= >=
左 ? :
左 ||
左 &&
结合方向 运算符
左 |
左 ^
左 &
无 == != === !==
无 >=
左 >
左 + - .
左 * / %
右 ! ~ ++ -- (int) (float) (string) (array) (object) @
右 [
无 new
The above is the detailed content of Introduction to various operators in php and examples. For more information, please follow other related articles on the PHP Chinese website!

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 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 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 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.

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 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.

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

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.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools