Detailed introduction to expressions and operators in php

怪我咯
Release: 2023-03-10 21:28:01
Original
1632 people have browsed it

php Operators and expressions

1. Classification of operators

1. Classification by operand

1.!true // Unary operator
2.$a+$b // Binary operator
3.true ? 1:0 // Ternary operator

2. Classification by operation function

(1) Arithmetic operators
1.+, -, x, /, % (remainder)

(2)String operator
1.. // For example: $a = 'abc'.'efg';

(3) Assignment operator

1.= // Simple assignment
2.+=, -=, X=, /=, %=, .= // Compound assignment
3. ++($a++, ++$a), --($a--, --$a) // Increment and decrement
4.&($a = 1;$b = &a) // Reference assignment

(4)Comparison operators

1.==, ===(equal to), !=, !===, <>( Not equal to), <, >, <=, >=

(5) Logical operator
1.// Ratio in parentheses Exceptions have high priority
2.&&(and), ||(or), !(not), xor(exclusive or),

(6)bit operators
1.& (bitwise AND), | (bitwise OR), ~ (bitwise NOT), ^ (bitwise XOR), << (left shift), >> (right shift)

2. Arithmetic operator

% remainder, common usage: 1) Integer division operation 2) Control the value range

Example: Determine whether it is a leap year (one leap year every four years, no leap years, and another leap year after four hundred years)

// % will convert the numbers on both sides into integers and then divide them
/ / % cannot use decimals or negative numbers on both sides
if ((($year%4 == 0) && ($year%100 != 0)) || $year%400 == 0)
echo "Leap year ";
else
echo "平年";

3. Assignment operator

//First add 10 to itself, and then assign it to Self, equivalent to $a=$a+10
$a += 10;

//Prefix increment and decrement, first increment and decrement and then assign value
++$a

//Assign value first, then increment and decrement
$a++
//Example
$a = 10;
$b = $a++
$c = --$b
Result: a=11 b=9 c=9

##4. Logical operators

xor exclusive or: the same is false (two true or two false = false), different values ​​are true (one true and one false = false)

Tips: Pay attention to the difference from or, or two true = true
Logical operator short circuit
1, && //One is false, then If no operation is performed later, it must be false
2, || //If one is true, then if no operation is performed later, it must be true

fopen ("test.php","r") or die(" Failed");

Tips: There is no short-circuit feature for operator & |

5. Bit operator

Bit operation: Convert integer type It is a 32-bit binary, and the string is converted to ANSCA code for processing

The above is the detailed content of Detailed introduction to expressions and operators in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!