Php operator usage instructions in the PHP Getting Started Tutorial

Source: Internet
Author: User
Tags php operator
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 include: 1. arithmetic operators 2. value assignment operator 3. comparison... 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:

 

The same is true for decline.

Value assignment operator

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

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:

 

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 a concatenation operator. For more information, 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:

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

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.

 

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 (? :) Features:

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

// Equivalent:

$person = emptyempty($who)? "laruence" : $who;

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

The difference between the constant operator and the comparison operator "=" is that "=" does not check the type of conditional expressions. the constant operator checks the value and type of expressions at the same time. this is mainly related to php's non-type language, 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 a null value such as "" or "0" is returned, 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.

  true var_dump("1" == "01"); // 1 == 1 -> true var_dump("1" == "1e0"); // 1 == 1 -> true switch ("a") { case 0: echo "0"; break; case "a": // never reached because "a" is already matched with 0 echo "a"; break; }?>

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

 

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

 

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

-----------------------------------------

$ A = 20; // 16 + 4 00000000 00000000 00000000 00010100

-----------------------------------------

$ B = 30; // 16 + 8 + 4 + 2 00000000 00000000 00000000

-----------------------------------------

$ C = $ a & $ B;

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:

 '; echo $str; echo '
'?>


Permanent address:

Reprint at will ~ Please bring the tutorial URL ^

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.