SoFunction
Updated on 2025-03-09

PHP learning operator related concepts


<?php
/* Operator symbol (PHP) operator symbol
*
* According to the operation symbol function, it is divided into:
* 1. Arithmetic operator + - * / % ++ --
* 2. String operator. Connection operator
* 3. Assignment operator = += -= *= /= %= .=
* 4. Comparison operator > < >= <= == == != <> !==
* Comparison operator---condition operator---relational operator
*There is only one result after comparison: boolean true false
* === When comparing, not only the same content is required, but also the same type is required.
* !== The contents are different when comparing, and the types are also required to be different.
* 5. Logical operator && or and || or or ! or not
* Logical operators can only operate bool-type values, and the returned bool-type values ​​are also returned.
* 6. Bit operator & | ^ ~ << >> >>>
* 7. Other operators ? : `` @ => -> :: & $
* `` is used to execute the operating system kernel
* @ Used to block error messages
* It is recommended to use "()" to change the priority of expressions
*
* % has two purposes: dividing; control the range, do not use decimals or negative numbers.
* % The numbers on both sides of the operator are converted into integers and then divided into the remaining.
*/
// Use the % symbol to judge a leap year
$year=2011;
if(($year%4==0 && %year%100!=0) || $year%400=0)
{
echo "run nian";
}
else
{
echo " not run nian";
}
// ++ --Usage of symbols
$a=10;
$a++; //$a=$a+1; Use variables first, then increment by 1
++$a; //$a=$a+1; First increase 1, use variables
$a--; //$a=$a-1; Use the variable first, then subtract 1 by itself
--$a; //$a=$a-1; Decrement by itself first, then use variables
echo $a; //The result is 10
//++ -- Differences in operations
$a=10;
$b=$a++;//b=10,a=11
$c=--$b;//c=9,b=9
$d=$c++ + ++$c; //d=20,c=11
$e=$d-- - --$d; //d=18,e=2
echo $d;
//Use of string operator .
$name="tom";
$age=27;
$height=1.75;
echo "My name is: {$name}My age is: {$age}My height is: {$height}m<br>";
echo 'My name is: '.$name.'My age is: '.$age.'My height is: '.$height.'m'.'<br>';
echo "\$age=".$age; //$age=27
echo "My name is: {$name}My age is: {$age}My height is: {$height}meter<br>";//Use of assignment operator
$a=10;
$a+=10; //$a=$a+10;
$a-=10; //$a=$a-10;
$a*=10; //...
$a/=10; //...
$a%=10; //$a=$a%10;
$a.="abc";//$a=$a."abc";
echo $a;
$str='<table>';
$str.='<tr>';
$str.='<td>';
$str.='</td>';
$str.='</tr>';
$str.='</table>';
echo $str;//Output a table
//Compare operator
var_dump(15>6);//Return bool(true)
$a=15;
if(15==$a)
{
echo "a=15";
}
else
{
echo "a!=15";
}
//Use of logical operators
var_dump(true && true);//true
var_dump(true && false);//false
var_dump(true || false);//true
var_dump(!true);//false
var_dump(!false);//true
//Judge username and password
$username="admin";
$password="123456";
$email="290080604@";
if($username=="admin" && $password="123456")
{
echo "Username and password are correct";
}
if($username=="" || $password=="" || $email=="")
{
echo "No one can be empty";
}
//Bit operator
$a=20; //00010100
$b=30; // 00011110
/*
* 20 00010100
* 30 00011110 &
*-----------------------------------
* 00010100
*
*/
$c=$a & $b;
echo $c;
/*Supplement, & | can also be used for logical operations
* && and || short circuit problem:
* &&When performing operations, if the previous number is false, then whether the following is true, the entire expression is false, so the following operand is not executed;
* ||When performing operations, if the previous number is true, whether the following number is false, the entire expression is true, so the subsequent operand is not executed;
* However, & or | When performing operations, both sides will be executed
*/
$a=10;
if($a>5 || $a++<100){}
echo $a;//Output 10
$b=10;
if($b>5 | $b++<100){}
echo $b;//Output 11
/*
The concept of bits: a bit is composed of 8 binary numbers (example 00000000).
If a byte consists of 8 bits, then there are 32 binary numbers.
Original code: The highest bit uses 0 to represent a positive number and 1 to represent a negative number
+7 00000111
-7 10000111
Inverse code: If a number is positive, its inverse code is the same as the original code;
If a number is negative, the sign bit is 1, and the rest are inversely denoting the original code;
+7 00000111
-7 11111000
+0 00000000
-0 11111111
Complement: If a number is positive, its complement and inverse code are the same as the original code.
If a number is negative, its complement = inverse code + 1, remove the overflow bit of the highest bit.
-7 Original code 100001111 Inverse code 11111000
+1
Complement code 11111001
A negative complement is known, convert it to a decimal number.
1. Reverse it to you first
2. Convert it to a decimal number
3. Add a negative sign and subtract 1.
Example: Complement code 11111010
Inverse 00000101
4+1=5
-5-1=-6
Bit operator:
& Bitwise and |Bitwise or ^Bitwise XOR ~Bitwise inverse
Example: bitwise and 01101101
&00110111
00100101
Conclusion: Only 1 1 is 1.
bitwise or 01101101
|00110111
01111111
Conclusion: Only 0 0 is 0.
Bitwise XOR 01101101
^00110111
01011010
Conclusion: When only 1 0 or 0 1 is 1. (It can also be understood that it is in a different state as 1 (true))
Bitwise invert ~00110111
11001000
Conclusion: Change 0 to 1, 1 to 0
Shift operator:
Move left: << Signed right:>> Unsigned right:>>
Example: Number x x<<2 x>>2 x>>>2
17 00010001 01000100 00000100 00000100
-17 11101111 10111100 11111011 00111011
Conclusion: Both positive numbers move left and right and right and right are complemented, negative numbers move left and right are complemented, and 0 is complemented without sign
*/
//Use of other operators
$a=10;
$b=$a>5 ? $a : 5;//Triple-part operator, if $b=$a otherwise $b=5
echo $b;
//Use `` to execute the operating system shell command
$str=`ipconfig /all`;
echo '<pre>';
echo $str;
echo '</pre>';
?>