Php operator usage instructions in the PHP Getting Started Tutorial

Source: Internet
Author: User
Tags bitwise operators php operator
In php, operators are the most basic knowledge point in our program development. next I will introduce you to some common usage of php operators in PHP.

In php, operators are the most basic knowledge point in our program development. next I will introduce some common usage of php operators in PHP.

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. $ X = 2;
  2. Echo $ x ++; // output 2
  3. Echo $ x; // output 3
  4. $ X = 2;
  5. Echo ++ $ x; // output 3
  6. Echo $ x; // output 3
  7. ?>

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. $ Y = ($ x = 2) + 5; // The result is $ x = 2, $ y = 7.
  2. ?>

How to use the deformation of the value assignment operator:

1 $ y + = $ x;

In fact, it is equivalent:

1 $ y = $ y + $ x;

Likewise:

$ Y-= $ x;

$ Y * = $ x;

$ Y/= $ x;

$ Y % = $ x;

$ 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 mainly refers to the concatenation operator ".", which connects the left and right parameter strings.

Example:

  1. $ X = "beijing ";
  2. $ 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:

 

  1. $ X = "beijing ";
  2. $ 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.

  1. /* Read the file */
  2. $ My_file = @ file ('file1') or
  3. Die ("failed to open the file, error message: '$ php_errormsg '");
  4. ?>

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:

What is output?

This question may be simple at first glance, but it is not easy to think carefully. if we say that Boolean and the previous part are due to priority issues, but if it is just 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:

Ask a question about the php operator priority. $ t = 1 & $ tt = 2 is executed according to the php operator priority ($ t = 1) & $ tt) = 2, but it should actually be ($ t = 1) & ($ tt = 2) I don't quite understand.

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 (? :) Functions: $ person = $ who or $ person = "laruence"; // equivalent:
  1. $ Person = emptyempty ($ 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". if they are equal to =, however, 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 = 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, if $ a is not equal to $ B.

$ A <> $ B is not equal to TRUE. if $ a is not equal to $ B.

$! = $ B is not fully equal to TRUE. if $ a is 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 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 an integer and a string are compared, the string is converted to an integer. if two numeric strings are compared, the string is compared as an integer. this rule is also applicable to switch statements.

  1. Var_dump (0 = "a"); // 0 = 0-> true
  2. Var_dump ("1" = "01"); // 1 = 1-> true
  3. Var_dump ("1" = "1e0"); // 1 = 1-> true
  4. Switch (""){
  5. Case 0:
  6. Echo "0 ";
  7. Break;
  8. Case "a": // never reached because "a" is already matched with 0
  9. Echo "";
  10. Break;
  11. }
  12. ?>

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, do not use negative numbers, % will convert the numbers on both sides into integers after division

  1. $ Year = 2010;
  2. If ($ yeay % 4 = 0 & $ year % 100! = 0 | $ year % 400 = 0 ){
  3. Echo "this year is a leap year ";
  4. }

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 -- $

  1. $ A = 10;
  2. $ B = $ a ++; // $ B = 10 $ a = 11
  3. $ C = -- $ B; // $ c = 9 $ B = 9
  4. // 9 + 11
  5. $ D = $ c ++ $ c; // $ d = 20 $ c = 11
  6. $ E = $ d ----- $ d; // 2
  7. $ Name = "ljw ";
  8. $ Age = 25;
  9. $ Heigh = 1.75;
  10. Echo "My name is: {$ name} my age: {$ age} my height {$ hegit }";
  11. Echo 'My name is: '. $ name.' my age: '. $ age.' my height '. $ hegit .;
  12. Echo "/$ age =". $ age; comparison operator --- conditional operator ----- relational operator

The comparison result only has one boolean true false.

=== Equality operator! ==----- === The comparison requires the same content and 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

  1. ------------------------------
  2. $ A = 20; // 16 + 4 00000000 00000000 00000000 00010100
  3. ------------------------------
  4. $ B = 30; // 16 + 8 + 4 + 2 00000000 00000000 00000000
  5. ------------------------------
  6. $ C = $ a & $ B;
  7. Echo $ c;

& | It can be both a logical operator and a bit operator & | short-circuit problem: & | short-circuit occurs & during computation, if the number of front edges is false, whether the backend is true, and the entire expression is false | if the number is true, whether the backend is false, the entire expression is true;

& Or | during operation, both sides are executed.

Other operators

Condition? Run it here: If not, run it here.

Run the operating system command in reverse quotes:

  1. $ Str = 'ipconfig/all ';
  2.  
  3. Echo'
    '; 
  4. Echo $ str;
  5. Echo''@ GetType (); the error control operator is placed in the front of the expression. if the expression is incorrect, it is blocked. it 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.