Home php教程 php手册 php常用的运算符 及操作符号详解

php常用的运算符 及操作符号详解

Jun 13, 2016 am 09:55 AM
php one Function according to operate Tutorial use symbol Detailed explanation Operation operator

/*运算符号(PHP)操作符号
 *
 * 按运算符号功能分为:
 * 一、算术运算符   + - * / % ++ --
 * 二、字符串运算符 . 连接运算符
 * 三、赋值运算符   = += -= *= /= %= .=
 * 四、比较运算符   > = !==
 *  比较运算符---条件运算符---关系运算符
 *  比较后的结果只有一种:boolean true false
 *  === 比较时不仅要求内容相同,也要求类型相同
 *  !== 比较时内容不相同,也要求类型不相同
 * 五、逻辑运算符   &&或and ||或or  !或not
 *  逻辑运算符只能操作bool型的值,返回的也是bool型的值
 * 六、位运算符     &  |  ^  ~  >  >>>
 * 七、其他运算符   ?  :  ``   @  =>  ->   ::  & $
 *  ``  用来执行操作系统内核
 *  @   用来屏蔽掉错误信息
 * 建议使用“()”改变表达式的优先级别
 *
 * % 有两个目的:整除运算;控制范围,不要用小数,也不要用负数
 * % 会吧运算符两边的数转为整数后再进行整除求余。
 */

 //用 %符号判断闰年
 

$year=2011;
 if(($year%4==0 && %year%100!=0) || $year%400=0)
 {
  echo "run nian";
 }
 else
 {
  echo " not run nian";
 }
 


 // ++  --符号的使用
 $a=10;
 $a++; //$a=$a+1; 先用变量,再自增1
 ++$a; //$a=$a+1; 先自增1,在用变量
 $a--; //$a=$a-1; 先用变量,再自减1
 --$a; //$a=$a-1; 先自减1,再用变量
 echo $a; //结果为10
 
 //++ -- 运算的区别

 $a=10;
 $b=$a++;//b=10,a=11
 $c=--$b;//c=9,b=9
 $d=$c++ + ++$c; //d=20,c=11
 $e=$d-- - --$d; //d=18,e=2
 echo $d;


 
 //字符串运算符 .   的使用
 $name="tom";
 $age=27;
 $height=1.75;
 echo "我的名字是:{$name}我的年龄是:{$age}我的身高是:{$height}米
";
 echo '我的名字是:'.$name.'我的年龄是:'.$age.'我的身高是:'.$height.'米'.'
';
 echo "$age=".$age; //$age=27
 
  echo "我的名字是:{$name}我的年龄是:{$age}我的身高是:{$height}米
";//赋值运算符的使用
 

$a=10;
 $a+=10; //$a=$a+10;
 $a-=10; //$a=$a-10;
 $a*=10; //...
 $a/=10; //...
 $a%=10; //$a=$a%10;
 $a.="abc";//$a=$a."abc";
 echo $a;
 $str='

';
 $str.='';
 $str.='';
 $str.='';
 $str.='
';
 $str.='
';
 echo $str;//输出一个表格

 //比较运算符
 var_dump(15>6);//返回 bool(true)
 $a=15;
 if(15==$a)
 {
  echo "a=15";
 }
 else
 {
  echo "a!=15";
 }
 
 

//逻辑运算符的使用

 var_dump(true && true);//true
 var_dump(true && false);//false
 var_dump(true || false);//true
 var_dump(!true);//false
 var_dump(!false);//true
 //判断用户名密码
 $username="admin";
 $password="123456";
 $email="290080604@qq.com";
 if($username=="admin" && $password="123456")
 {
  echo "用户名密码正确";
 }
 if($username=="" || $password=="" || $email=="")
 {
  echo "一个都不能为空";
 }

 

//位运算符
 $a=20;  //00010100
 $b=30; // 00011110
 /*
  *   20  00010100
  *   30  00011110      &
  *-----------------------------------
  *  00010100
  *
  */
 $c=$a & $b;
 echo $c;


 /*补充,&  |  也可以用做逻辑运算
  * &&和||的 短路问题:
  *  &&在作运算时,如果前面的数为false,则后面是否为true,整个表达式都为false,所以就不去执行后面的操作数;
  *  ||在作运算时,如果前面的数为true,则后面的数是否为false,整个表达式都为true,所以就不去执行后面的操作数;
  *  然而,& 或者 | 在作运算时,两边都会被执行
  */
 $a=10;
 if($a>5 || $a++   echo $a;//输出10
 $b=10;
 if($b>5 | $b++   echo $b;//输出11


 /*
位的概念:一个位是由8个二进制数组成的(例00000000),
 一个字节由8个位组成,那么就有32个二进制数。


原码:最高位 用0表示正数,1表示负数

+7  00000111
-7  10000111


反码:一个数如果为正,则它的反码与原码相同;
      一个数如果为负,则符号位为1,其余各位是对原码取反;
+7  00000111
-7  11111000

+0  00000000
-0  11111111

补码:一个数如果为正,则它的补码与反码与原码相同
      一个数如果为负,则它的补码=反码+1,去掉最高位的溢出位
     
      -7  原码 10000111  反码11111000
                                     +1
                           补码11111001
                          
已知一个负数的补码,把它转换为十进制数。
1.先对各位取反
2.将其转换为十进制数
3.加上负号,再减去1。

例:补码11111010
    取反00000101
             4+1=5
    -5-1=-6
   
位运算符:
& 按位与     |按位或    ^按位异或 ~按位取反

例:  按位与   01101101
              &00110111
               00100101
               结论:只有1 1为1。
      按位或   01101101
              |00110111
               01111111
               结论:只有0 0为0。
      按位异或 01101101
              ^00110111
               01011010
               结论:只有1 0或0 1时为1。(也可以理解为处于不同状态为1(真))
      按位取反 ~00110111
                11001000
                结论:将0变1,1变0


移位运算符:
 左移:>     无符号右移:>>>
 
 例:数   x         x>2     x>>>2
     17 00010001   01000100    00000100  00000100
    -17 11101111   10111100    11111011  00111011
    结论:正数左右移动都补0,负数左移补0,带符号右移补1,不带符号补0
 
 
*/

 //其他运算符的运用
 $a=10;
 $b=$a>5 ? $a : 5;//三元运算符,如果成立$b=$a否则$b=5
 echo $b;
 
 //用``来执行操作系统shell命令
 $str=`ipconfig /all`;
 echo '

';<br>
 echo $str;<br>
 echo '
Copy after login
';


?>

本部分列出了在 PHP 中使用的各种运算符:

算数运算符

运算符 说明 例子 结果
+ Addition x=2
x+2
4
- Subtraction x=2
5-x
3
* Multiplication x=4
x*5
20
/ Division 15/5
5/2
3
2.5
% Modulus (division remainder) 5%2
10%8
10%2
1
2
0
++ Increment x=5
x++
x=6
-- Decrement x=5
x--
x=4

赋值运算符

运算符 说明 例子
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= x%=y x=x%y

比较运算符

运算符 说明 例子
== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
> is greater than 5>8 returns false
is less than 5
>= is greater than or equal to 5>=8 returns false
is less than or equal to 5

逻辑运算符

运算符 说明 例子
&& and x=6
y=3

(x 1) returns true

|| or x=6
y=3

(x==5 || y==5) returns false

! not x=6
y=3

!(x==y) returns true

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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

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

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

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

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