PHP8: PHP operator

Source: Internet
Author: User
Tags php operator
PHP8: PHP operators This chapter describes the PHP operators.
The operator problem exists in every language, because we are familiar with one or more programming languages, so we just need to know about it.
To sum up.
There are many PHP operators, which seem to be more popular than C/C ++ and C. They are:
Arithmetic operator value assignment operator bit operator comparison operator error control operator execution operator increment/decrease operator logical operator string operator array operator type operator this is also quite diverse.

Arithmetic operators

Example

Name

Result

-$

Invert

The negative value of $.

$ A + $ B

Addition

Sum of $ a and $ B.

$ A-$ B

Subtraction

The difference between $ a and $ B.

$ A * $ B

Multiplication

The product of $ a and $ B.

$ A/$ B

Division

$ A divided by $ B.

$ A % $ B

Modulo

Divide $ a by the remainder of $ B.

Bitwise operators

Example

Name

Result

$ A & $ B

And (bitwise And)

Set the bits of $ a and $ B to 1.

$ A | $ B

Or (by bit Or)

Set the bit where $ a or $ B is 1 to 1.

$ A ^ $ B

Xor (by bit OR)

The bitwise of $ a and $ B is set to 1.

~ $

Not (non-by-bit)

Set the bits 0 in $ a to 1, and vice versa.

$ A <$ B

Shift left (left Shift)

Move the bits in $ a to the left for $ B (each movement represents "multiplied by 2 ").

$ A> $ B

Shift right (right Shift)

Move the bits in $ a to the right $ B (each movement indicates "divided by 2 ").

Comparison Operators

Example

Name

Result

$ A = $ B

Equal

TRUE, if $ a is equal to $ B.

$ A ===$ B

Full

TRUE. if $ a is equal to $ B and their types are the same. (Introduced in PHP 4)

$! = $ B

Not supported

TRUE, if $ a is not equal to $ B.

$ A <> $ B

Not supported

TRUE, if $ a is not equal to $ B.

$! ==$ B

Incomplete

TRUE. if $ a is not equal to $ B, or they are of different types. (Introduced in PHP 4)

$ A <$ B

Xiaohe

TRUE, if $ a is strictly less than $ B.

$ A> $ B

Greater

TRUE. if $ a is strict with $ B.

$ A <= $ B

Less than or equal

TRUE, if $ a is less than or equal to $ B.

$ A> = $ B

Greater than or equal

TRUE, if $ a is greater than or equal to $ B.

Increment/decrease operator

Example

Name

Effect

++ $

Add

Add one to the value of $ a, and then return $.

$ A ++

Houjia

Returns $ a and adds the value of $ a to one.

-- $

Minus

The value of $ a is reduced by one, and $ a is returned.

$ --

Subtraction

$ A is returned, and then the value of $ a is reduced by one.

Logical operators

Example

Name

Result

$ A and $ B

And (logical And)

TRUE. if both $ a and $ B are TRUE.

$ A or $ B

Or (logical Or)

TRUE. if $ a or $ B is set to TRUE.

$ A xor $ B

Xor (logical exclusive or)

TRUE: if $ a or $ B is set to TRUE, but not both.

! $

Not (logical non)

TRUE. if $ a is not TRUE.

$ A & $ B

And (logical And)

TRUE. if both $ a and $ B are TRUE.

$ A | $ B

Or (logical Or)

TRUE. if $ a or $ B is set to TRUE.

Array operators

Example

Name

Result

$ A + $ B

Union

$ A and $ B.

$ A = $ B

Equal

TRUE if $ a and $ B have the same key/value pair.

$ A ===$ B

Full

TRUE if $ a and $ B have the same key/value pairs and the order and type are the same.

$! = $ B

Not supported

TRUE if $ a is not equal to $ B.

$ A <> $ B

Not supported

TRUE if $ a is not equal to $ B.

$! ==$ B

Incomplete

TRUE if $ a is not fully equal to $ B.

Of course there are several types that are not listed, which will be described later. Let's take a look at the list.
If the preceding operators are not described in detail, they are similar to those in C.
First, let's look at the Shift left (<) or shift right (>) in bitwise operations. In fact, there is no difference, but the symbols are different. Let's take a look at "=" and "=" in the comparison calculation method. What are the differences between these two values? Take a look at the code:

1 2 var_dump (0 = "a"); // 0 = 0-> true
3 var_dump ("1" = "01"); // 1 = 1-> true
4
5 switch (""){
6 case 0:
7 echo "0 ";
8 break;
9 case "a": // never reached because "a" is already matched with 0
10 echo "";
11 break;
12}

Why is 0 equal to "a" when I look at row 2nd? Strange. In PHP, if an integer is compared with a string, the string is automatically converted to a value. Here is a question: What is the principle of converting a string to a value? The principle is

1) If the string contains any of the characters ".", "e", or "E", it is evaluated as a float. Otherwise, it is treated as an integer.

2) the value is determined by the first part of the string. If a string starts with a valid number, use this number as its value. Otherwise, its value is 0 (0 ). Valid numeric data starts with an optional plus or minus sign, followed by one or more numbers (including decimal scores), followed by an optional index. An index is an "e" or "E" followed by one or more numbers. For example:

1 2 $ var = 1 + "10.5 ";
3 echo "1 + \" 10.5 \ "=". $ var. "; type:". gettype ($ var )."
";
4 $ var = 1 + "-1.3e3 ";
5 echo "1 + \"-1.3e3 \ "=". $ var. "; type:". gettype ($ var )."
";
6 $ var = 1 + "bob-1.3e3 ";
7 echo "1 + \" bob-1.3e3 \ "=". $ var. "; type:". gettype ($ var )."
";
8 $ var = 1 + "bob3 ";
9 echo "1 + \" bob3 \ "=". $ var. "; type:". gettype ($ var )."
";
10 $ var = 1 + "10 beatiful birds ";
11 echo "1 + \" 10 beatiful birds \ "=". $ var. "; type:". gettype ($ var )."
";
12 $ var = 4 + "10.2 Little Apple ";
13 echo "4 + \" 10.2 Little Apple \ "=". $ var. "; type:". gettype ($ var )."
";
14 $ var = "10.0 pigs" + 1;
15 echo "\" 10.0 pigs \ "+ 1 =". $ var. "; type:". gettype ($ var )."
";
16 $ var = "10.0 pigs" + 1.0;
17 echo "\" 10.0 pigs \ "+ 1.0 =". $ var. "; type:". gettype ($ var )."
";
18?>
19

The output result is:

1 + "10.5" = 11.5; type: double
1 + "-1.3e3" =-1299; type: double
1 + bob-1.3e3 = 1; type: integer
1 + "bob3" = 1; type: integer
1 + "10 beatiful birds" = 11; type: integer
4 + "10.2 Little Apple" = 14.2; type: double
"10.0 pigs" + 1 = 11; type: double
"10.0 pigs" + 1.0 = 11; type: double

Now I understand what is going on. This is why "a" is 0.
In this case, there is also a "=", three equal to the symbol, it means not only the final converted value is equal, but also the type should be equal. Take a look at the following code:

1 2 var_dump (0 = "a"); // 0 = 0-> false
3 echo'
';
4 var_dump (0 = "00"); // 0 = "00"-> false
5 echo'
';
6 var_dump (0 = 0); // 0 = 0-> true
7 echo'
';
8?>

Output result:

Bool (false)
Bool (false)
Bool (true)

The reason why 6th rows are true is that they are not only of the same value but also of the same type.
3) let's take a look at the logic operation. "or" and "and" have two writing methods. if you use C/C ++, C #, Java, and other languages, use "&" or "|. If you are using VB, use and, and or. Of course you can also use both. What are their differences? The priority is different ." & "And" | "is higher than" and "and" or.
4). PHP has an array operator. With the above explanation, I believe they are not very difficult.

Like the C # language, the value assignment operator also uses "=" and binary symbols to assign values. For example:

$ A = 'hello'; // $ a is hello.
$ A + = 1; // $ a is 1, binary value assignment
$ B = 'world ';
$ C = 'hello ';
$ C. = $ B; // $ c is "hello world"
?>


The error execution operator PHP supports an error control operator :@. Before placing the expression in a PHP expression, any error information that may be generated by the expression is ignored.

If the track_errors feature is activated, any error messages generated by the expression are stored in the variable $ php_errormsg. This variable will be overwritten every time an error occurs, so if you want to use it, you should check it as soon as possible. For example:

/* Intentional file error */
$ My_file = @ file ('non_existent_file ') or
Die ("Failed opening file: error was '$ php_errormsg '");

// This works for any expression, not just functions:
$ Value = @ $ cache [$ key];
// Will not issue a notice if the index $ key doesn't exist.

?>

The execution operator PHP supports an execution operator: unquoted quotation marks (''). Note that this is not a single quotation mark! PHP will try to execute the content in the back quotes as a shell command and return its output information (for example, it can be assigned to a variable rather than simply discarded to the standard output ). The effect of using the unquoted operator "'" is the same as that of the shell_exec () function. The quotation marks are enclosed in the quotation marks (~) of the keyboard "~ "Key. For example:

1 2 $ out = 'dir c :';
3 echo'

 ' . $out . ' 
';
4?>

Result:

Volume in drive C has no label.
Volume Serial Number is A019-7D77

Directory of C :\

02/24/2006 10: 21 PM Downloads
02/24/2006 10: 09 PM Kingsoft Temp Download
05/13/2006 07: 07 PM 220 cmd. txt
02/13/2006 11: 40 PM WINDOWS
02/13/2006 11: 47 PM Documents and Settings
02/13/2006 11: 57 PM Program Files
06/24/2006 11: 01 PM 482,933 nfzmLog. log
05/11/2006 10: 28 PM Inetpub
05/13/2006 07: 07 PM 4 response. txt
07/29/2006 05: 56 PM 0 Rec. pcm
06/24/2006 09: 05 PM nf
07/29/2006 05: 56 PM 0 WriteAudrv. wav
07/04/2006 08: 52 PM dwnSetup
07/08/2006 09: 28 AM TEMP
02/14/2006 12: 15 AM NVIDIA
5 File (s) 483,157 bytes 10 Dir (s) 2,116,558,848 bytes free



Increment/decrease operator

PHP supports the C-style ascending and descending operators.

Note: the increment/decrease operator does not affect Boolean values. Decreasing the NULL value does not work, but the result of increasing the NULL value is 1.

Increment/decrease operator

Example name effect
++ $ Add Add one to the value of $ a, and then return $.
$ A ++ Houjia Returns $ a and adds the value of $ a to one.
-- $ Minus The value of $ a is reduced by one, and $ a is returned.
$ -- Subtraction Returns $ a and then drops the value of $.
Type operators

PHP has a type operator: instanceof. Instanceof is used to determine whether a given object comes from a specified object class.

The instanceof operator is introduced in PHP 5. Is_a () was used before, but is_a () is outdated. it is best to use instanceof.

Is in C #, while Java is instanceof, which is the same as PHP. For example:

1 2 class {}
3 class B {}
4
5 $ thing = new;
6
7 if ($ thing instanceof ){
8 echo 'A ';
9}
10 if ($ thing instanceof B ){
11 echo 'B ';
12}
13?>


The character operator has two character string operators. The first is the concatenation operator ("."), which returns the string after the left and right parameters are connected. The second is the concatenation of the value assignment operator (". ="), which attaches the parameter on the right to the parameter on the left. As we have seen above, we will not give an example.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.