[PHP] data type & operator & bit operation

type of data

Data type: data type, in PHP, refers to the type of the stored data itself, not the type of the variable. **PHP is a weakly typed language, variables themselves have no data types. ** Divide data into three categories and eight subcategories in PHP:

Simple (basic) data types: 4 subclasses

Integer type: int/integer, the system allocates 4 bytes for storage, indicating the integer type (conditional)

Floating-point type: float/double, the system allocates 8 bytes of storage, indicating decimals or integers that cannot be stored in integers

String type: string, the system allocates according to the actual length, indicating a string (quotation marks)

Boolean type: bool/boolean, representing Boolean type, only two values: true and false

Composite data types: 2 subclasses

Object type: object, storing objects (object-oriented)

Array type: array, store multiple data (one-time)

Special data types: 2 subclasses

Resource type: resource, store resource data (PHP external data, such as database, file)

Empty type: NULL, only one value is NULL (cannot be operated)


type conversion

Type conversion: Under many conditions, the specified data type is required, external data (currently obtained by PHP) is required, and converted to the target data type

There are two types of conversion in PHP:

1. Automatic conversion: the system judges and converts by itself according to the needs (it is used more, and the efficiency is low)

2. Mandatory (manual) conversion: consider conversion according to the required target type

Mandatory conversion rules: Add a parenthesis () before the variable, and then write the corresponding type inside: int/integer.... The NULL type uses unset(). In the conversion process, the more used is

  • Convert to Boolean type (judgment) and convert to numeric type (arithmetic operation)
  • Other types are converted to Boolean types: true or false, and relatively few types are converted to false in PHP

image-20230731140240524


Explanation of other types of rotation values

1. Boolean true is 1, false is 0;

2. Converting strings to values ​​has its own rules

2.1 A string starting with a letter is always 0;

2.2 A string starting with a number, until the string is encountered (does not contain two decimal points at the same time)

$a = 'abc1.1.1';
$b = '1.1.1.abc';

echo $a + $b; //自动转化   1.1
echo '<hr/>',(float)$a,'<br/>',(float)$b;  #0  1.1

type judgment

A set of type judgment functions are used to judge the variable, and finally return the data type of the data stored in the variable (the same result is true, and the failure is false): a set of functions starting with is_ followed by the type name: is_XXX(variable name )

The bool type cannot be viewed with echo, but can be viewed with the var_dump structure

  • var_dump(variable1,variable2...)
$a = 'abc1.1.1';
$b = '1.1.1.abc';
var_dump(is_int($a)); #bool(false) 
var_dump(is_string($b));#bool(true)

There is also a set of functions that can be used to get and set the type of data (variables)

  • Gettype (variable name): Get the type, and get the string corresponding to the type

  • Settype (variable name, type): set data type: different from mandatory conversion

    • 1. Mandatory conversion (type) variable name is to process the content copied from the data value (the actual stored content will not be processed)
    • 2. settype will directly change the data itself
$a = 'abc1.1.1';
$b = '1.1.1.abc';
echo gettype($a); #string

var_dump(settype($b,'int')); #bool(true)    #settype设置类型成功,返回true
echo gettype($b); #integar

integer type

Integer type: save integer value (range limit), 4 bytes to store data, the maximum is 32 bits: 2^32 = more than 4.2 billion (unsigned number). But in PHP, the default is a signed type (distinguish between positive and negative numbers)

Four integer definitions are provided in PHP: decimal definition, binary definition, octal definition and hexadecimal definition

$a1 = 120;		//10进制
$a2 = 0b110;	    //2进制
$a3 = 0120;		//8进制
$a4 = 0x120;	    //16进制
echo $a1,'<hr/>', $a2,'<hr/>', $a3,'<hr/>', $a4,'<hr/>';  #120  6   80  288

Conversion

Decimal system: every 10 enters 1, and the numbers that can appear are 0-9

Binary: Every 2 enters 1, and the numbers that can appear are 0-1

Octal: every 8 enters 1, and the numbers that can appear are 0-7

Hexadecimal: enter 1 every 16, the numbers that can appear are 0-9 and af, a means 10, and so on


PHP does not require users to perform such complicated calculations, and provides many functions for conversion (binary: bin octal: oct decimal: dec hexadecimal: hex)

  • decbin(): convert decimal to binary
  • decoct(): convert decimal to octal
  • dechex(): convert decimal to hexadecimal
  • bindec(): Binary to decimal

floating point type

Floating-point type: decimal type and integers that exceed the storage range of integer types (no guarantee of accuracy), the accuracy range is about 15 significant figures

There are two ways to define floating point

  • $f = 1.23;

  • $f = 1.23e10; //Scientific notation, where e means base 10

$f1 = 1.23;
$f2 = 1.23e10;
var_dump($f1,$f2); #float(1.23)  float(12300000000)

Try not to use floating-point numbers to make accurate judgments: the data saved by floating-point numbers is not accurate enough, and basically all decimals in the computer are not accurate

$f1 = 0.7;
$f2 = 2.1 / 3;
var_dump($f1 == $f2); #bool(false)

Boolean type

Boolean type: two values ​​true and false, usually used for judgment comparison. When making certain data judgments, special attention needs to be paid to type conversion

  • empty(): Determine whether the value of the data is "empty" , not NULL, if it is empty, return true, if it is not empty, return false
  • isset(): Determine whether the variable in the data storage itself exists , return true if the variable exists, and return false if it does not exist

image-20230731141820018

$a;
var_dump(isset($a)); //bool(false)
var_dump(empty($a)); //bool(true)

$x = NULL;
var_dump(isset($x)); //bool(false)
var_dump(empty($x)); //bool(true)

operator

Operator: operator, which is a special symbol for performing operations on data

assignment operator

Assignment operation: the symbol is "=", which means that the result on the right (which can be the result of variables, data, constants and other operations) is saved to a certain location in the memory, and then the memory address of the location is assigned to the variable on the left (constant).

arithmetic operator

Arithmetic Operations: Basic Arithmetic Operations

  • +: Execute data accumulation
  • -: Data subtraction
  • * : There is no multiplication symbol on the keyboard, use * to multiply two numbers
  • /: Forward slash instead, means to divide two numbers
  • %: Remainder (modulo) operation, divide two numbers (integers), and keep the remainder

When performing division or remainder operation, the corresponding divisor (second number) cannot be 0


comparison operator

Comparison operation: compare the size of two data, or whether the two contents are the same, the returned result is Boolean type: true if satisfied, false if not satisfied

  • >: The left side is greater than the right side, and the result is true
  • >=: the left is greater than or equal to the right
  • <: the left is smaller than the right
  • <=: the left is less than or equal to the right
  • ==: the one on the left is the same as the one on the right (same size)
  • !=: The one on the left is different from the one on the right (different size)
  • ===: all equal, the left and right are the same: the size and data type must be the same
  • !==: Not all equal, only different in size or type
$a = 123; //整形
$b = '123';//字符串
var_dump($a == $b);  #bool(true) 
var_dump($a === $b); #bool(false)  大小和类型都要相同

Logical Operators

Logical operations: Match against different results. If the condition is met, return true, if not, return false

  • &&: Logical AND, the condition on the left and the condition on the right are true at the same time (both results are true)
  • ||: logical or, as long as one of the conditions on the left or the condition on the right is satisfied
  • ! : Logical NOT, negates the existing condition, if it is true, the result of negation is false

Logical AND and logical OR are also called short-circuit operations: if the result of the first expression already satisfies the condition, then the expression after the logical operator will not be run


join operator

Connection operation: It is a symbol for splicing multiple strings in PHP

  • .Two strings can be concatenated using
  • .=Compound operation, connect the content on the left with the content on the right, and then reassign the value to the variable on the left

For example: A .= B ==>A = A.b

$a = 'hello';
$b = 'Mango';
echo $a.$b; #helloMango

echo '<hr/>';
$a .= $b;
echo $a; #helloMango

error suppressor

There are some errors in PHP that can be predicted in advance, but these errors may be unavoidable, but if you don’t want to report errors to users, you can use error suppressors to deal with them.

@: Just use the @ symbol in front of the expression that may make an error.

  • Error suppressors are usually used in the production environment (online), not in development: the system itself is preferably free of any errors.
$a = 10;
$b = 0;
@($a / $b); //如果没有错误抑制:报错Warning: Division by zero in D:\apache\htdocs\index.php on line 86

Ternary operator

Trinocular operation: an operation involving three expressions (simple branch structure abbreviation)

Format:表达式1 ? 表达式2 :表达式3;

  • If expression 1 is true, execute expression 2, otherwise execute expression 3

Note: If the expression itself is complex, it is recommended to use parentheses to wrap it

$a = 10;
$b = $a >= 10 ? 20 : 10;
echo $b ; //20

It can be known that the ternary operator has a higher priority than the assignment operator

self-operating operator

Post-self-operation: first keep the value saved by yourself, and then change yourself, and the value you give to others is the original value;

Pre-self-operation: first change yourself, and then give the changed value to others.

$a = 0;
$b = 0;
echo $a++,'<br/>',++$b; // 0 1 
echo '<br/>',$a,'<br/>',$b;// 1 1

Derived symbols: similar to self-operating

+=: The result on the left is added to the result on the right, and then assigned to the left

-=: Subtract the result from the left to the right, and then copy it to the left

*=: multiplication operation

/=: division operation

%=: modulo operation

Notice:

1. The content on the right is a whole

$a = 10;
$b = 5;
$a -= $b-1; //相当于:$a = $a - ($b-1)
echo $a;//6

2. If division or remainder operation is performed, then consider whether the result of the expression on the right is 0 (error for 0)


computer code

Computer code: the binary encoding rules adopted by the computer when actually storing data

  • Computer code: original code, complement code and complement code
  • The leftmost bit of the value itself is used as a sign bit: a positive number is 0, and a negative number is 1

bit operation

&: bitwise AND, both bits are 1, the result is 1, otherwise it is 0

|: bitwise or, one of the two is 1, and the result is 1

~: Bitwise not, if a bit is 1, it becomes 0, otherwise vice versa

^: bitwise XOR, 0 if two are the same, 1 if they are different

<<: Shift left bit by bit, the whole bit (32 bits), move one bit to the left, and add 0 to the right (multiply by 2)

>>: bitwise right shift, the whole bit is moved one bit to the right, and the corresponding content of the sign bit is complemented on the left (positive numbers are complemented with 0, negative numbers are complemented with 1) (the operation of dividing by 2 (not completely correct, because integers divided by 2 will appear decimals) )

Notice:

1. When the system performs any bit operation, it uses the complement code

2. After the operation is completed, it must be converted into the original code to be the final data to be displayed


operator precedence problem

Operator precedence: how to combine operations when multiple operators exist at the same time

image-20230731150154150

Guess you like

Origin blog.csdn.net/chuxinchangcun/article/details/132450799