Operators for PHP 8:php

Source: Internet
Author: User
Tags bitwise operators
This chapter describes the PHP operators.
Operator this is a problem in every language, because we are already familiar with one or more of the programming languages, so we just need to know.
Summarize it.
There are many kinds of PHP operators, which seem to be more than c/c++,c# and other languages. respectively:
Arithmetic operator assignment operator bitwise operator comparison operator error control operator execute operator increment/decrement operator logical operator string operator array operator type operator This is also quite a variety of.

Arithmetic operators

Example

Name

Results

-$a

Take counter

The negative value of the $a.

$a + $b

Addition

$a and $b and.

$a-$b

Subtraction

The difference between $a and $b.

$a * $b

Multiplication

The product of $a and $b.

$a/$b

Division

$a divided by the quotient of the $b.

$a% $b

Take the mold

$a divided by the remainder of the $b.

Bitwise operators

Example

Name

Results

$a & $b

and (Bitwise AND)

A bit of 1 in both $a and $b will be set to 1.

$a | $b

or (bitwise OR)

The 1 bit in the $a or $b is set to 1.

$a ^ $b

XOR (Bitwise XOR)

The different bits in the $a and $b will be set to 1.

~ $a

Not (bitwise non)

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

$a << $b

Shift left

Moves the bits in the $a to the left $b times (each move represents "multiplied by 2").

$a >> $b

Shift Right (move to left)

Moves the bits in the $a to the right $b times (each move represents "divided by 2").

Comparison operators

Example

Name

Results

$a = = $b

Equals

TRUE if the $a equals $b.

$a = = = $b

Congruent

TRUE if $a equals $b, and they are of the same type. (introduced in PHP 4)

$a! = $b

Range

TRUE if $a is not equal to $b.

$a <> $b

Range

TRUE if $a is not equal to $b.

$a!== $b

Non-congruent

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

$a < $b

Small and

TRUE if $a is strictly less than $b.

$a > $b

Greater than

TRUE if the $a strictly $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/decrement operator

Example

Name

Effect

+ + $a

Front Plus

The value of the $a is added one, and then the $a is returned.

$a + +

Post-add

Return $a, and then add a value of $a.

--$a

Pre-minus

The value of the $a is reduced by one and then returned to the $a.

$a--

Post-subtraction

Return $a, and then subtract the value of $a by one.

logical operators

Example

Name

Results

$a and $b

and (Logical AND)

True if $a and $b are true.

$a or $b

or (logical OR)

True if either $a or $b is true.

$a XOR $b

Xor (Logical XOR)

True if either $a or $b is true, but not at the same time.

! $a

Not (logical)

True if the $a is not true.

$a && $b

and (Logical AND)

True if $a and $b are true.

$a | | $b

or (logical OR)

True if either $a or $b is true.

Array operators

Example

Name

Results

$a + $b

Joint

The Union of $a and $b.

$a = = $b

Equal

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

$a = = = $b

Congruent

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

$a! = $b

Range

TRUE if the $a is not equal to $b.

$a <> $b

Range

TRUE if the $a is not equal to $b.

$a!== $b

Not congruent

TRUE if the $a is not all equal to $b.

Of course there are several types that are not listed and will be described later. Let's look at the list first.
If there are no special instructions for the operators above, then they are similar to those in C #.
First, look at the left shift (<<) or right Shift (>>) in the bitwise operation, but it's not the same, it's just a different symbol. Look at the comparison algorithm in the "= =", "= = =", these two are more interesting, what is the difference between them? 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 ("a") {
6 Case 0:
7 echo "0";
8 break;
9 case ' a ':///Never reached because "a" is already matched with 0
echo "a";
Break one by one;
12}

Look at line 2nd, why is 0 equal to "a"? It's strange. is equal because in PHP, strings are automatically converted to numeric values when integers and strings are compared. Here's a question: what is the principle of converting a string into a numeric value? Principle is

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

2) This value is determined by the front part of the string. If the string starts with a valid numeric data, the number is used as its value, otherwise its value is 0 (0). Valid numeric data starts with an optional sign, followed by one or more digits (optionally including a decimal fraction) followed by an optional exponent. An exponent 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). "
" ;
Ten $var = 1 + "beatiful birds";
echo "1 + \" Ten beatiful birds\ "=". $var. " ; Type: ". GetType ($var). "
" ;
$var = 4 + "10.2 Little Apple";
echo "4 + \" 10.2 Little apple\ "=". $var. " ; Type: ". GetType ($var). "
" ;
$var = "10.0 pigs" + 1;
echo "\" 10.0 pigs \ "+ 1 =". $var. " ; Type: ". GetType ($var). "
" ;
$var = "10.0 pigs" + 1.0;
+ echo "\" 10.0 pigs \ "+ 1.0 =". $var. " ; Type: ". GetType ($var). "
" ;
?>
19

The result of the output 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 + "Ten 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 you know what's going on. Why "A" is 0 is the truth.
In this case, there is also a "= = =", 3 equals sign, which means that not only the final conversion of the values are 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 = = = False for "XX"
5 Echo '
' ;
6 Var_dump (0 = = 0); 0 = = = 0 True
7 Echo '
' ;
8?>

The output is:

BOOL (FALSE)
BOOL (FALSE)
BOOL (TRUE)

The 6th line is true because they are not only equal in value, but also in the same type.
3) Look at the logical operation, "or" and "with" actually have 2 kinds of wording, if you use C/c++,c#,java and other languages, use "&&" or "| |" it. If you are VB, use and, and OR. Of course you can also use 2 kinds. What difference do they have? The difference between priorities. "&&" and "| |" Higher than the "and" and "or" precedence.
4). PHP has one more array operator. With the above explanations, it is not very difficult to believe them.

The assignment operator, like the C # language, also uses "=" and is also assigned a value using a two-tuple symbol. For example:

$a = ' Hello '; $a is hello.
$a + = 1; $a is 1, two Yuan assignment
$b = ' world ';
$c = ' Hello ';
$c. = $b; $c is "Hello World"
?>


Error execution operator PHP supports an error control operator: @. Any error messages that may be generated by the expression are ignored until it is placed in a PHP expression.

If the track_errors attribute is activated, any error information generated by the expression is stored in the variable $php _errormsg. This variable is overwritten every time an error occurs, so check it out as soon as possible if you want to use it. For example:

/* Intentional File Error */
$my _file = @ file (' non_existent_file ') or
Die ("Failed opening file:error is ' $php _errormsg '");

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

?>

The Execute operator PHP supports an execution operator: an inverse quotation mark ('). Note that this is not a single quote! PHP will attempt to execute the contents of the anti-quote as a shell command and return its output information (for example, it can be assigned to a variable instead of simply discarding it to standard output). The effect of using the inverse quote operator "'" is the same as the function shell_exec (). The back quotation marks are located in the "~" Key of the keyboard. For example:

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

' ;
4?>

The result is:

Volume in Drive C have 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 + 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 Dir (s) 2, 558, 848 bytes free



Increment/decrement operator

PHP supports C-style pre/post increment and decrement operators.

Note: The increment/decrement operator does not affect Boolean values. Decreasing the null value also has no effect, but the result of incrementing null is 1.

Increment/decrement operator

Example name effect
+ + $a Front Plus The value of the $a is added one, and then the $a is returned.
$a + + Post-add Return $a, and then add a value of $a.
--$a Pre-minus The value of the $a is reduced by one and then returned to the $a.
$a-- Post-subtraction Returns the $a and then the value of the $a minus
Type operator

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

The instanceof operator was introduced in PHP 5. Prior to this, is_a () was used, but is_a () was obsolete, preferably with instanceof.

C # is, and Java is instanceof, like PHP. For example:

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


The character operator has two string operators. The first one is the join operator (".") ), which returns the string after its left and right arguments are concatenated. The second is the Join assignment operator (". ="), which attaches the right argument to the left argument. These have been seen in the front, not for 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.