search
Homephp教程php手册前端学PHP之运算符,学PHP之运算符
前端学PHP之运算符,学PHP之运算符Jun 13, 2016 am 08:48 AM
phpfront endstringstudycontrolCompareTable of contentsarithmeticAssignmentoperatorIncrementlogicmistake

前端学PHP之运算符,学PHP之运算符

目录
算术运算符 赋值运算符 比较运算符 三元运算符 逻辑运算符 字符串运算符 错误控制运算符 递增递减运算符 数组运算符

定义

  运算符即做运算的标识符号。PHP运算符一般分为算术运算符、赋值运算符、比较运算符、三元运算符、逻辑运算符、字符串连接运算符、错误控制运算符、递增递减运算符和数组运算符

 

算术运算符

+<span>(加法)
    </span><span>$x</span> + <span>$y</span>
-<span>(减法)
    </span><span>$x</span> - <span>$y</span>
*<span>(乘法)
    </span><span>$x</span> * <span>$y</span>
/<span>(除法)
    </span><span>$x</span> / <span>$y</span>
%<span>(求模)
    </span><span>$x</span> % <span>$y</span>

 

赋值运算符

  PHP的赋值运算符有两种,分别是直接赋值"="和引用赋值"&"

[1]直接赋值

  直接赋值"="把右边表达式的值赋给左边的运算数。它将右边表达式值复制一份,交给左边运算数。换言之,首先给左边运算数申请一块内存,然后把复制的值放到这块内存中

x =<span> y
x </span>+=<span> y
x </span>-=<span> y
x </span>*=<span> y
x </span>/=<span> y
x </span>%= y

[2]引用赋值

  引用赋值&意味着两个变量都指向同一个数据。它将使两个变量共享一块内存,如果这个内存存储的数据变了,那么两个变量的值都会发生变化

<?<span>php 
    </span><span>$a</span> = "测试内容1"<span>;
    </span><span>$b</span> = <span>$a</span><span>;
    </span><span>$c</span> = &<span>$a</span><span>;
    </span><span>$a</span> = "测试内容2"<span>;
    </span><span>echo</span> <span>$b</span>."<br />";<span>//</span><span>测试内容1</span>
    <span>echo</span> <span>$c</span>."<br />";<span>//</span><span>测试内容2</span>
?>

 

比较运算符

  比较运算符主要用来进行比较运算

==<span>         等于 
</span>===<span>        全等
</span>!=<span>         不等
</span><><span>         不等
</span>!==<span>        不全等
</span>><span>          大于
</span><<span>          小于
</span>>=<span>         大于等于
</span><=         小于等于
<?<span>php  
    </span><span>$a</span> = 1<span>;
    </span><span>$b</span> = "1"<span>;
    </span><span>var_dump</span>(<span>$a</span> == <span>$b</span><span>);
    </span><span>echo</span> "<br />";<span>//</span><span>bool(true)</span>
    <span>var_dump</span>(<span>$a</span> === <span>$b</span><span>);
    </span><span>echo</span> "<br />";<span>//</span><span>bool(false)</span>
    <span>var_dump</span>(<span>$a</span> != <span>$b</span><span>);
    </span><span>echo</span> "<br />";<span>//</span><span>bool(false)</span>
    <span>var_dump</span>(<span>$a</span> <> <span>$b</span><span>);
    </span><span>echo</span> "<br />";<span>//</span><span>bool(false)</span>
    <span>var_dump</span>(<span>$a</span> !== <span>$b</span><span>);
    </span><span>echo</span> "<br />";<span>//</span><span>bool(true)</span>
    <span>var_dump</span>(<span>$a</span> < <span>$b</span><span>);
    </span><span>echo</span> "<br />";<span>//</span><span>bool(false)</span>
?>

 

三元运算符

  "?:"三元运算符是一个比较运算符,对于表达式(expr1)?(expr2):(expr3),如果expr1的值为true,则此表达式的值为expr2,否则为expr3

<?<span>php 
    </span><span>$a</span> = 78;<span>//</span><span>成绩</span>
    <span>$b</span> = <span>$a</span> >=60?"及格":"不及格"<span>;
    </span><span>echo</span> <span>$b</span>;<span>//</span><span>及格</span>
?>

 

逻辑运算符

  逻辑运算符主要是进行逻辑运算

<span>and        与
or         或
xor        异或
</span>&&<span>         与
</span>||<span>         或
</span>!          非
<?<span>php 
    </span><span>$a</span> = <span>TRUE</span>; <span>//</span><span>A同意</span>
    <span>$b</span> = <span>TRUE</span>; <span>//</span><span>B同意</span>
    <span>$c</span> = <span>FALSE</span>; <span>//</span><span>C反对</span>
    <span>$d</span> = <span>FALSE</span>; <span>//</span><span>D反对</span>
    <span>echo</span> (<span>$a</span> and <span>$b</span>);<span>//</span><span>1</span>
    <span>echo</span> "<br />"<span>;
    </span><span>echo</span> (<span>$a</span> or <span>$c</span>);<span>//</span><span>1</span>
    <span>echo</span> "<br />"<span>;
    </span><span>echo</span>(<span>$a</span> xor <span>$c</span> xor <span>$d</span>);<span>//</span><span>1</span>
    <span>echo</span> "<br />"<span>;
    </span><span>echo</span>(!<span>$c</span> ? "通过" :"不通过");<span>//</span><span>通过</span>
    <span>echo</span> "<br />"<span>;
    </span><span>echo</span>(<span>$a</span> && <span>$d</span> ? "通过":"不通过");<span>//</span><span>不通过</span>
    <span>echo</span> "<br />"<span>;
    </span><span>echo</span> (<span>$b</span> || <span>$c</span> || <span>$d</span> ? "通过":"不通过");<span>//</span><span>通过</span>
?>

 

字符串运算符

  字符串连接运算符是为了将两个字符串进行连接

[1]连接运算符(.)

<?<span>php 
    </span><span>$a</span> = 'hello'<span>;
    </span><span>$b</span> = <span>$a</span> . ' world!'<span>;
    </span><span>//</span><span>hello world!</span>
    <span>echo</span> <span>$b</span><span>;
</span>?>

[2]连接赋值运算符(.=)

<?<span>php 
    </span><span>$x</span> = 'hello'<span>;
    </span><span>$x</span> .= ' world!'<span>;
    </span><span>//</span><span>hello world!</span>
    <span>echo</span> <span>$x</span><span>;
</span>?>

 

错误控制运算符

  PHP提供了一个错误控制运算符@,对于一些可能会有运行过程中出错的表达式,不希望出错时显示错误信息时,将@放置在一个PHP表达式之前。如果激活了track_error特性,表达式产生的任何错误信息都被存放在变量$php_errormsg中,此变量在每次出错时都会被覆盖

  [注意]错误控制前缀@不会屏蔽解析错误的信息,不能把它放在函数或类的定义之前,也不能用于条件结构如if和foreach等

<?<span>php
</span><span>$a</span> = 1<span>;
</span><span>echo</span> @ <span>$a</span>;<span>//</span><span>1</span>
<span>$b</span><span>;
</span><span>echo</span> @ <span>$b</span>;<span>//</span><span>不报错</span>
?>

 

递增/递减运算符

++<span>$x</span><span>     前递增
</span><span>$x</span>++<span>     后递增
</span>--<span>$x</span><span>     前递减
</span><span>$x</span>--     后递减
<?<span>php
</span><span>$x</span>=10<span>; 
</span><span>echo</span> ++<span>$x</span>; <span>//</span><span> 输出 11</span>
<span>$y</span>=10<span>; 
</span><span>echo</span> <span>$y</span>++; <span>//</span><span> 输出 10</span>
<span>$z</span>=5<span>;
</span><span>echo</span> --<span>$z</span>; <span>//</span><span> 输出 4</span>
<span>$i</span>=5<span>;
</span><span>echo</span> <span>$i</span>--; <span>//</span><span> 输出 5</span>
?>

 

数组运算符

  用于比较数组

+<span>      联合
</span>==<span>     相等
</span>===<span>   全等
</span>!=<span>     不相等
</span><><span>     不相等
</span>!==   不全等
<?<span>php
</span><span>$x</span> = <span>array</span>("a" => "red", "b" => "green"<span>); 
</span><span>$y</span> = <span>array</span>("c" => "blue", "d" => "yellow"<span>); 
</span><span>$z</span> = <span>$x</span> + <span>$y</span><span>; 
</span><span>var_dump</span>(<span>$z</span>);<span>//</span><span>array(4) { ["a"]=> string(3) "red" ["b"]=> string(5) "green" ["c"]=> string(4) "blue" ["d"]=> string(6) "yellow" } </span>
<span>echo</span> "<br>"<span>;
</span><span>var_dump</span>(<span>$x</span> == <span>$y</span>);<span>//</span><span>bool(false)</span>
<span>echo</span> "<br>"<span>;
</span><span>var_dump</span>(<span>$x</span> === <span>$y</span>);<span>//</span><span>bool(false)</span>
<span>echo</span> "<br>"<span>;
</span><span>var_dump</span>(<span>$x</span> != <span>$y</span>);<span>//</span><span>bool(true)</span>
<span>echo</span> "<br>"<span>;
</span><span>var_dump</span>(<span>$x</span> <> <span>$y</span>);<span>//</span><span>bool(true)</span>
<span>echo</span> "<br>"<span>;
</span><span>var_dump</span>(<span>$x</span> !== <span>$y</span>);<span>//</span><span>bool(true)</span>
?> 

 

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
巧用CSS实现各种奇形怪状按钮(附代码)巧用CSS实现各种奇形怪状按钮(附代码)Jul 19, 2022 am 11:28 AM

本篇文章带大家看看怎么使用 CSS 轻松实现高频出现的各类奇形怪状按钮,希望对大家有所帮助!

实战:vscode中开发一个支持vue文件跳转到定义的插件实战:vscode中开发一个支持vue文件跳转到定义的插件Nov 16, 2022 pm 08:43 PM

vscode自身是支持vue文件组件跳转到定义的,但是支持的力度是非常弱的。我们在vue-cli的配置的下,可以写很多灵活的用法,这样可以提升我们的生产效率。但是正是这些灵活的写法,导致了vscode自身提供的功能无法支持跳转到文件定义。为了兼容这些灵活的写法,提高工作效率,所以写了一个vscode支持vue文件跳转到定义的插件。

5个常见的JavaScript内存错误5个常见的JavaScript内存错误Aug 25, 2022 am 10:27 AM

JavaScript 不提供任何内存管理操作。相反,内存由 JavaScript VM 通过内存回收过程管理,该过程称为垃圾收集。

Node.js 19正式发布,聊聊它的 6 大特性!Node.js 19正式发布,聊聊它的 6 大特性!Nov 16, 2022 pm 08:34 PM

Node 19已正式发布,下面本篇文章就来带大家详解了解一下Node.js 19的 6 大特性,希望对大家有所帮助!

浅析Vue3动态组件怎么进行异常处理浅析Vue3动态组件怎么进行异常处理Dec 02, 2022 pm 09:11 PM

Vue3动态组件怎么进行异常处理?下面本篇文章带大家聊聊Vue3 动态组件异常处理的方法,希望对大家有所帮助!

聊聊如何选择一个最好的Node.js Docker镜像?聊聊如何选择一个最好的Node.js Docker镜像?Dec 13, 2022 pm 08:00 PM

选择一个Node​的Docker镜像看起来像是一件小事,但是镜像的大小和潜在漏洞可能会对你的CI/CD流程和安全造成重大的影响。那我们如何选择一个最好Node.js Docker镜像呢?

聊聊Node.js中的 GC (垃圾回收)机制聊聊Node.js中的 GC (垃圾回收)机制Nov 29, 2022 pm 08:44 PM

Node.js 是如何做 GC (垃圾回收)的?下面本篇文章就来带大家了解一下。

【6大类】实用的前端处理文件的工具库,快来收藏吧!【6大类】实用的前端处理文件的工具库,快来收藏吧!Jul 15, 2022 pm 02:58 PM

本篇文章给大家整理和分享几个前端文件处理相关的实用工具库,共分成6大类一一介绍给大家,希望对大家有所帮助。

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

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

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.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version