Table of Contents
Learn PHP operators on the front end, learn PHP operators
Directory
Definition
Arithmetic operators
Assignment operator
Comparison operator
Ternary operator
Logical operators
String operators
Error control operator
Increment/Decrease Operator
Array operator
Home Backend Development PHP Tutorial Learn PHP operators on the front end, learn PHP operators_PHP tutorial

Learn PHP operators on the front end, learn PHP operators_PHP tutorial

Jul 12, 2016 am 09:01 AM
php front end string study control Compare Table of contents arithmetic Assignment operator Increment logic mistake

Learn PHP operators on the front end, learn PHP operators

Directory
Arithmetic operators Assignment operators Comparison operators Ternary operators Logical operators String operators Error control operators Increment and decrement operators Array operators

Definition

Operators are identifiers for performing operations. PHP operators are generally divided into arithmetic operators, assignment operators, comparison operators, ternary operators, logical operators, string concatenation operators, error control operators , increment and decrement operators and array operators

Arithmetic operators

+<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>
Copy after login

Assignment operator

There are two types of assignment operators in PHP, namely direct assignment "=" and reference assignment "&"

[1] Direct assignment

Direct assignment "=" assigns the value of the expression on the right to the operand on the left. It copies the value of the expression on the right and gives it to the operand on the left. In other words, first apply for a memory for the left operand, and then put the copied value into this memory

x =<span> y
x </span>+=<span> y
x </span>-=<span> y
x </span>*=<span> y
x </span>/=<span> y
x </span>%= y
Copy after login

[2] Reference assignment

Reference assignment & means that both variables point to the same data. It will make two variables share a memory. If the data stored in this memory changes, the values ​​​​of both variables will change

<?<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>
?>
Copy after login

Comparison operator

Comparison operators are mainly used to perform comparison operations

==<span>         等于 
</span>===<span>        全等
</span>!=<span>         不等
</span><><span>         不等
</span>!==<span>        不全等
</span>><span>          大于
</span><<span>          小于
</span>>=<span>         大于等于
</span><=         小于等于
Copy after login
<?<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>
?>
Copy after login

Ternary operator

"?:" The ternary operator is a comparison operator. For the expression (expr1)?(expr2):(expr3), if the value of expr1 is true, then the value of this expression is expr2 , otherwise 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>
?>
Copy after login

Logical operators

Logical operators mainly perform logical operations

<span>and        与
or         或
xor        异或
</span>&&<span>         与
</span>||<span>         或
</span>!          非
Copy after login
<?<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>
?>
Copy after login

String operators

The string concatenation operator is used to concatenate two strings

[1] Concatenation operator (.)

<?<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>?>
Copy after login

[2] Concatenation assignment operator (.=)

<?<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>?>
Copy after login

Error control operator

PHP provides an error control operator @. For some expressions that may cause errors during operation, and you do not want the error message to be displayed when an error occurs, place @ before a PHP expression. If the track_error feature is activated, any error messages generated by the expression are stored in the variable $php_errormsg, which will be overwritten on each error

[Note] The error control prefix @ will not block parsing error information. It cannot be placed before the definition of a function or class, nor can it be used in conditional structures such as if and 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>
?>
Copy after login

Increment/Decrease Operator

++<span>$x</span><span>     前递增
</span><span>$x</span>++<span>     后递增
</span>--<span>$x</span><span>     前递减
</span><span>$x</span>--     后递减
Copy after login
<?<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>
?>
Copy after login

Array operator

Used to compare arrays

+<span>      联合
</span>==<span>     相等
</span>===<span>   全等
</span>!=<span>     不相等
</span><><span>     不相等
</span>!==   不全等
Copy after login
<?<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>
?> 
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1087142.htmlTechArticleLearn PHP operators on the front end, learn PHP operator directory arithmetic operator assignment operator comparison operator ternary Operator Logical Operator String Operator Error Control Operator Increment Increment...
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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months 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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

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

See all articles