search
HomeBackend DevelopmentPHP TutorialPHP学习 运算符与运算符优先级_php基础

算术运算符
  运算符      名称    结果            
  $a + $b      加法    $a 和 $b 的和
  $a - $b      减法    $a 和 $b 的差
  $a * $b      乘法    $a 和 $b 的积
  $a / $b      除法    $a 除以 $b 的商
  $a % $b      取模    $a 除以 $b 的余数
递增/递减运算符
  运算符    名称       结果
  ++$a   前加    $a 的值加一,然后进行操作
    $a++     后加    $a 的值先进行操作,后加一
    --$a   前减    $a 的值减一,然后进行操作
    $a--   后减    $a 的值先进行操作,后减一
实例:
  echo  $a=5+”5th”;    //输出:10
  echo  10%3;          //输出:1
  echo  10+ $a++;      //输出:20
  echo   5- --$a;        //输出:-5
?>
比较运算符
  运算符          名称         结果
  $a == $b      等于       TRUE,如果$a等于$b
  $a === $b      全等        TRUE,如果$a等于$b,并且它们的类型也相同
  $a != $b         不等       TRUE,如果$a不等于$b
  $a  $b      不等          TRUE,如果$a不等于$b
  $a !== $b       非全等    TRUE,如果$a不等于$b,或者它们的类型不同
  $a   $a > $b         大于        TRUE,如果$a严格$b
  $a   $a >= $b      大于等于  TRUE,如果 $a 大于或等于$b
  另外一个条件运算符是“  ?   :  ”(或三元)运算符。

实例:
     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 
?>
逻辑运算符
  运算符      名称      结果
  $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解释程序将不会计算右边的表达式:
        $a=10;
      if(false && (++$a));
      echo $a;      //输出:10
      $b=10;
      if(true or (++$b));
      echo $b;      //输出:10
  ?>
位运算符
  位运算符允许对整型数中指定的位进行置位。如果左右参数都是字符串,则位运算符将操作字符的 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”)。
    其他运算符
字符串运算符
有两个字符串运算符。第一个是连接运算符(“.”),它返回其左右参数连接后的字符串。第二个是连接赋值运算符(“.=”),它将右边参数附加到左边的参数后。
错误抑制操作符
在最常见的数据库连接与文件创建操作或出现除0等异常时,可以用@符号来抑制函数错误信息输出到浏览器端 $a=@(5/0)
  外部命令执行
       使用``来运行外部系统命令,注意不是单引号,是ESC下面那个按键
                  $out=`dir c:`;
              print_r($out);
        ?>//不建议使用
实例:
  $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  
?>
    运算符优先级 
下表从低到高列出了运算符的优先级。 
  结合方向    运算符
  左      ,
  左      or
  左      xor
  左      and
  右      print
  右      = += -= *= /= .= %= &= |= ^=                      ~=   >= 
  左      ? :
  左      ||
  左      &&
结合方向        运算符
  左      |
  左      ^
  左      &
  无      == != === !==
  无       >= 
  左      >
  左      +  -  .
  左      *  /   %
  右      ! ~ ++ -- (int) (float) (string) (array) (object) @
  右      [
  无      new

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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

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 Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool