Php operator usage instructions in the PHP Getting Started Tutorial

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise bitwise operators constant logical operators php operator


PHP operators

PHP operators mainly include:

1. Arithmetic operators
2. Value assignment operator
3. Comparison operators
4. Ternary operators
5. Logical operators
6. String operators
7. Error control operators

Arithmetic operators

Operator Description Example calculation result
+ Add $ x = 2;
$ Y = $ x + 1; $ y = 3
-Minus $ x = 2;
$ Y = $ X-1; $ y = 1
* Multiply by $ x = 2;
$ Y = $ x * 3; $ y = 6
/In addition, the returned result is of the floating point type $ x = 6;
$ Y = $ x/3; $ y = 2
% Perform remainder. The returned result is of the floating point type $ x = 6;
$ Y = $ x % 4; $ y = 2
++ Increment, divided into prefix and postfix
Invalid for Boolean and NULL values $ x = 2;
+ + $ X; (add before)
$ X ++; (add later) $ x = 3
-Decline, divided into descending and descending
Invalid for Boolean and NULL values $ x = 2;
-$ X; (minus)
$ X-; (minus) $ x = 1

For prefix and Postfix, the result after execution is a variable plus 1. The difference is that the returned results are different during execution. See the following two examples:


1 <? Php

2 $ x = 2;

3 echo $ x ++; // output 2

4 echo $ x; // output 3

5   

6 $ x = 2;

7 echo ++ $ x; // output 3

8 echo $ x; // output 3

9?>
The same is true for decline.

Value assignment operator
The value assignment operator "=" is used for value assignment. The value assignment operator assigns the value on the right to the variable on the left:


1 $ x = 2;
In this example, the integer value 2 is assigned to the variable $ x.

Other usage of the value assignment operator
Value assignment operators can be nested:


1 <? Php

2 $ y = ($ x = 2) + 5; // The result is $ x = 2, $ y = 7.

3?>
How to use the deformation of the value assignment operator:


1 $ y + = $ x;
In fact, it is equivalent:

 

1 $ y = $ y + $ x;
Likewise:


1 $ y-= $ x;

2 $ y * = $ x;

3 $ y/= $ x;

4 $ y % = $ x;

5 $ y. = $ x; //. Is the concatenation operator. For details, see string operators.
Comparison Operators

Operator Description Example calculation result
= 2 = 3 FALSE
! = Not equal to, can also be written <> 2 = 3 TRUE
> Greater than 2> 3 FALSE
<Less than 2 <3 TRUE
>=Greater than or equal to 2> = 3 FALSE
<= Less than or equal to 2 <= 3 TRUE

Comparison operators can also be used for string comparison.

Ternary operators
Three elements can be considered as special comparison operators:


()? (B): (C)
Syntax explanation: When A is evaluated as TRUE, the value of the entire expression is B; otherwise, it is C.

Example:

 

1 $ y = ($ x = 2 )? $ X: 1;

This example checks whether the value of the variable $ x is equal to 2. If $ x is equal to 2, the value of $ y is equal to $ x (that is, 2 ), otherwise, $ y equals 1.

Logical operators

Operator Description Example calculation result

& Logic and, you can also write and $ x = 2;
$ Y = 6;
$ X & $ y> 5 FALSE

| Logic or, which can also be written or $ x = 2;
$ Y = 6;
$ X & $ y> 5 TRUE

! Non-logical, take the opposite of the Logic $ x = 2;
$ Y = 6;
! ($ X> $ y) TRUE

Join operator
The string operator is the concatenation operator ".", which connects the left and right parameter strings.

Example:


$ X = "beijing ";

$ Y = $ x. "Hello"; // $ y = "Hello, beijing"


As shown in this example, the concatenation operator is required when the characters and variables are output together.

The connector and the value assignment operator can form a join value assignment operator:


$ X = "beijing ";

$ X. = "Hello"; // $ x = "Hello, beijing"

Error Control Operator
Error Control Operator: @. When it is placed before a PHP expression, any error information that may be generated by this expression is ignored.

<? Php
/* Read the file */
$ My_file = @ file ('file1') or
Die ("failed to open the file, error message: '$ php_errormsg '");
?>

The error control operator is often used to block PHP system errors and prompt friendly error messages where errors may occur.


An exception to the operator priority


Today, I saw a problem in Lao Wang's technical manual:

<? Phpif ($ a = 100 & $ B = 200) {var_dump ($ a, $ B );}

What is output?

This question may seem simple at first glance, but it is not easy to think carefully,

If Boolean and the previous part are due to priority issues, but if it is only a matter of priority, the result should be:

$ A = (100 & $ B) = 200 actually, the result is indeed a high-priority & concession to the next priority =, so that $ B = 200 is first combined.

The reason is that PHP does not fully comply with the definition of priority, which is also described in the PHP manual:

Note: Although = has a lower precedence than most other operators, PHP will still allow expressions similar to the following: if (! $ A = foo (), in which case the return value of foo () is put into $.

In this design, I personally do not express my opinion. In C language, such similar statements are determined as incorrect syntax. PHP adopts such a design, which may be due to historical reasons,

Curious people may want to know why. Previously, jayeeliu asked:

Hello laruence:
Question about the php operator priority
$ T = 1 & $ tt = 2
According to the php operator, the priority should be
($ T = 1) & $ tt) = 2
This sequence is executed, but it should actually be
($ T = 1) & ($ tt = 2)
I don't quite understand it.

In fact, it is also simple. Operator priority is a method for selecting protocol rules when there is a semantic grammar. In the syntax analysis file definition of PHP, so that there is no protocol conflict between the equal sign and T_BOOLEAN_AND:

Expr_without_variable: // a hidden rule exists, which is equivalent to T_BOOLEAN_AND becoming the "unary operator". | expr T_BOOLEAN_AND {zend_do_boolean_and_begin (& $1, & $2 tsrmls );}

Exp finally, by the way, PHP corresponds to T_BOOLEAN_AND also defines T_LOGICAL_AND (and) and T_LOGICAL_OR (or). The priority of these two values is lower than the equal sign, so there will be, the following are typical examples of PHP tutorials:

$ Result = mysql_query (*) or die (mysql_error ());

Similarly, we can use or to implement the ternary operator (? :) Features:

$ Person = $ who or $ person = "laruence ";

// Equivalent:

$ Person = empty ($ who )? "Laruence": $ who;

 

Difference between the constant equality operator (=) and the comparison operator (=) in php


The difference between the constant equal operator and the comparison operator "=" is that "=" does not check the type of the conditional expression. The constant equal operator checks the value and type of the expression at the same time.
This is mainly related to php non-type languages, such as NULL, FALSE, array (), "", 0, "0 & Prime; if they are equal, but if you determine the true return value of a value, you can use =
$ A = 0;
$ B = FALSE;
$ A = FALSE is not true, but $ B = FALSE is true. If yes, $ a = FALSE and $ B = FALSE are both true.

The ===operator is still very useful. Some built-in functions in php return a value if they succeed, and false if they fail, if it succeeds but the return value is a null value such as "" or 0, how do you determine the success or failure? You can use = to distinguish variable types.

Refer:

Comparison operator example name result
$ A = $ B is equal to TRUE if $ a is equal to $ B.
$ A ===$ B all equal to TRUE, if $ a is equal to $ B, and their types are the same. (Introduced in PHP 4)
$! = $ B is not equal to TRUE, if $ a is not equal to $ B.
$ A <> $ B is not equal to TRUE, if $ a is not equal to $ B.
$! = $ B is not equal to TRUE. If $ a is not equal to $ B, or they have different types. (Introduced in PHP 4)
$ A <$ B small and TRUE, if $ a is strictly less than $ B.
$ A> $ B is greater than TRUE if $ a is strict with $ B.
$ A <= $ B is less than or equal to TRUE, if $ a is less than or equal to $ B.
$ A >=$ B is greater than or equal to TRUE if $ a is greater than or equal to $ B.

If you compare an integer with a string, the string is converted to an integer. Compare two numeric strings as integers. This rule also applies to switch statements.

<? Php

Var_dump (0 = "a"); // 0 = 0-> true

Var_dump ("1" = "01"); // 1 = 1-> true

Var_dump ("1" = "1e0"); // 1 = 1-> true

Switch (""){

Case 0:

Echo "0 ";

Break;

Case "a": // never reached because "a" is already matched with 0

Echo "";

Break;

}

?>


After talking about a bunch of things, let's look at another article.


Operator Number (php) operator symbol

By operator number

■ Arithmetic operator +-*/% + +-
■ String operator (concatenation operator ).
■ Value assignment operator = + =-= * = % =. =
■ Comparison (relational and conditional) operator ><========! = <>! =
■ Logical operators & and | or! Not
■ Bitwise operators & | ~ ^ <>>>>
■ Other budget characters? : "->->::&$
■ () Change priority
% Has two purposes: Division operation; control range, do not use decimal places or negative numbers
% Will convert the numbers on both sides into integers and divide them in the integer
<? Php
$ Year = 2010;
If ($ yeay % 4 = 0 & $ year % 100! = 0 | $ year % 400 = 0 ){
Echo "this year is a leap year ";
}
Control scope
$ Num = 4600009;
Echo $ num % 10; // 9 the remainder of any number and 10 won't exceed 9
$ A ++ (first use the variable and then add 1) ++ $ a (first use the variable to add 1) $ a -- $
<? Php
$ A = 10;
$ B = $ a ++; // $ B = 10 $ a = 11
$ C = -- $ B; // $ c = 9 $ B = 9
// 9 + 11
$ D = $ c ++ $ c; // $ d = 20 $ c = 11
$ E = $ d ----- $ d; // 2
$ Name = "ljw ";
$ Age = 25;
$ Heigh = 1.75;
Echo "My name is: {$ name} my age: {$ age} my height {$ hegit }";
Echo 'My name is: '. $ name.' my age: '. $ age.' my height '. $ hegit .;
Echo "/$ age =". $ age; comparison operator --- conditional operator ----- relational operator
The comparison result only has one boolean true false.
=== Equality operator
! =
----- === Not only requires the same content, but also the same type
& Bitwise and (1 for both) | bitwise OR (1 for one) ^ exclusive or (0 for different values )~ Reverse (0 and 1 swaps) shifted left <, shifted right> unsigned> shifted right
------------------------------
$ A = 20; // 16 + 4 00000000 00000000 00000000 00010100
------------------------------
$ B = 30; // 16 + 8 + 4 + 2 00000000 00000000 00000000
------------------------------
$ C = $ a & $ B;
Echo $ c;
& And | can be both logical operators and bitwise operators
& |
Short circuit problem: & | short circuit may occur.
& When performing an operation, if the number of the front edge is false, whether the back end is true, and the entire expression is false
| The operation is performed. If the previous number is true, whether the latter is false, and the entire expression is true;
& Or | the operation is executed on both sides.
Other operators
Condition? Run it here: If not, run it here.
Run the operating system command"

$ Str = 'ipconfig/all ';
Echo '<pre> ';
Echo $ str;
Echo '</pre>' @ getType (); the error control operator is placed in the front of the expression. If the expression is incorrect, it is blocked. This parameter is not recommended.

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.