Contents
- Text
- Logical operators work on Boolean values
- not Operators:
- 2 singular operators
php editor Xigua brings you a guide to using examples of operators in PHP. This guide is designed to help both beginners and experienced developers better understand the usage and practical examples of various operators in PHP. Through this guide, you will learn how to correctly use arithmetic operators, comparison operators, logical operators and other operators in PHP, as well as their specific application scenarios in actual programming. Let’s dive deeper into the world of PHP operators!
Text
Once you have some variables, you can perform operations on them:
$base = 20;
$height = 10;
$area = $base * $height;
The * I use to multiply base and height is the multiplication operation .
We have quite a few operators, let’s give a brief summary of the main ones.
First, here are the arithmetic operators. ,-,*,/ (division), % (remainder) and ** (exponent).
We have the assignment operator =, which we have used to assign a value to a variable.
Next we have comparison operators, such as ,=. These operators work just like in mathematics:
2 < 1; //false
1 <= 1; // true
1 <= 2; // true
== Returns true if the two operands are equal.
=== Returns "true" if the two operands are the same.
What's the difference?
You will find out as you gain experience, but for example:
1 == '1'; //true
1 === '1'; //false
We also have != to detect whether the operands are not equal:
1 != 1; //false
1 != '1'; //false
1 != 2; //true
//hint: <> works in the same way as !=, 1 <> 1
and !== , to detect whether the operands are different:
1 !== 1; //false
1 !== '1'; //true
php Xiaobian Xigua brings you an example guide for using operators in PHP. This guide is designed to help both beginners and experienced developers better understand the usage and practical examples of various operators in PHP. Through this guide, you will learn how to correctly use arithmetic operators, comparison operators, logical operators and other operators in PHP, as well as their specific application scenarios in actual programming. Let’s dive deeper into the world of PHP operators!
Logical operators work on Boolean values
// Logical AND with && or "and"
true && true; //true
true && false; //false
false && true; //false
false && false; //false
true and true; //true
true and false; //false
false and true; //false
false and false; //false
// Logical OR with || or "or"
true || true; // true
true || false //true
false || true //true
false || false //false
true or true; // true
true or false //true
false or true //true
false or false //false
// Logical XOR (one of the two is true, but not both)
true xor true; // false
true xor false //true
false xor true //true
false xor false //false
php editor Xigua brings you a guide to using examples of operators in PHP. This guide is designed to help both beginners and experienced developers better understand the usage and practical examples of various operators in PHP. Through this guide, you will learn how to correctly use arithmetic operators, comparison operators, logical operators and other operators in PHP, as well as their specific application scenarios in actual programming. Let’s dive deeper into the world of PHP operators!
not operator:
$test = true
!$test //false
I used the boolean values true and false here, but in practice you would use expressions that evaluate to true or false, say:
1 > 2 || 2 > 1; //true
1 !== 2 && 2 > 2; //false
All operators listed above are binary, meaning they involve two operands.
php editor Xigua brings you a guide to using examples of operators in PHP. This guide is designed to help both beginners and experienced developers better understand the usage and practical examples of various operators in PHP. Through this guide, you will learn how to correctly use arithmetic operators, comparison operators, logical operators and other operators in PHP, as well as their specific application scenarios in actual programming. Let’s dive deeper into the world of PHP operators!
2 singular operators
PHP also has 2 singular operators: and --:
$age = 20;
$age++;
//age is now 21
$age--;
//age is now 20
The above is the detailed content of Detailed guide to operator usage examples in PHP. For more information, please follow other related articles on the PHP Chinese website!