How are the PHP operators categorized according to different criteria?

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

The PHP operator can be divided into unary operators, two-ary operators, and ternary operators based on the number of operands. Unary operators such as ! (take the inverse operator) or ++ (plus an operator), most of the operators supported by PHP are such binary operators, such as,, + , and - * / so on, while the ternary operator has only one ( ?: ). In addition, by the function of the operator to classify, can be divided into: arithmetic operators, string operators, assignment operators, comparison operators, logical operators, bitwise operators and other operators.

1. Arithmetic operators

Operator Meaning Writing format
+ Addition operation $a + $b
- Subtraction operations $a-$b
* Multiplication operations $a * $b
/ Division operation $a/$b
% Take Division remainder operation $a% $b
++ Since the increase of 1, such as $a++, is actually $a= $a +1 $a + +
-- Self minus 1, for example $ A--, is actually $a= $a-1 $a--

In the arithmetic operator, there is an increment and decrement rule that almost all programs have, as follows:

    • $a++Evaluates the value of the expression first and then adds 1 to itself

    • $a--The value of the expression is evaluated first, and then the self is reduced by 1

    • ++$aAdd 1 First, and then calculate the value of the expression

    • --$aSubtract 1 First, then calculate the value of the expression

Instance:

<?php    $count =1;    echo $count + +;  This line prints out 1    echo + + $count;  This line prints out the 3?>

2. String operators

In PHP, there is only one string operator, which is an English period ("."), also known as a join operator. This operator can not only concatenate two strings into a new merged string, but also concatenate a string with any scalar data type and merge it into a new string.

<?php$name = "Kevin";                Define a person's name as a string type $age = x;                      Define a person's age as integral type $height = 1.85;                 Define a person's height as floating-point type//Use the dot operator and string to concatenate the variables above, and output echo "My name is:". $name. ", My Age is:". $age. ", my height." $height. " Meters. "." <br> ";? >

3. Assignment operators

    • =Assigns the result of a value or expression to a variable

    • +=The result of adding the variable to the assigned value is then assigned to the variable

    • -=Assigns the result to the variable that subtracts the most from the assigned value

    • *=Assigns the result to the variable that is the most multiplied by the assigned value.

    • /=Assigns the result that divides the most to the assigned value to the variable

    • %=Assigns the result of the variable that divides the most to the assigned value to the

    • .=Assigns the result to the variable that is most connected to the assigned value

<?php $a = $b = $c = $d =;            The values for $a, $b, $c, and $d are 20$a + = 5;                      Equivalent to $a = $a +5; $b-= 5;                      Equivalent to $b = $b-5; $c *= 5;                      Equivalent to $c = $c; $d/= 5;                      Equivalent to $d = $d/5; $e%= 5;                      Equivalent to $e = $e%5; $result = "The result is:"; $result. = "\ $a the value after 5 is: ${a},"; $result. = "\ $b self-minus 5 after the value is: ${b},"; $result. = "\ $c squared 5 After the value is: ${ c}, "; $result. =" \ $d since the value after 5 is: ${d}, "; $result. =" \ $e The value after 5 fetch is: ${e}. "; Echo $result;                       Output of all concatenated string results?>

4. Comparison operators

    • ; is greater than, returns TRUE when the left operand is greater than the right operand. Otherwise, FALSE

    • < is less than, and returns TRUE when the left operand is less than the right-hand operand. Otherwise FALSE

    • >= is greater than or equal, and returns TRUE when the left operand is greater than or equal to the right operand. Otherwise FALSE

    • <= is less than or equal, and returns TRUE when the left operand is less than or equal to the right operand. Otherwise FALSE

    • = = equals, returns TRUE when the left operand equals the right operand. Otherwise FALSE

    • = = = all equals, when the left operand equals to the right operand, and their type also returns TRUE . Otherwise FALSE

    • <> or ! = is not equal to return TRUE when the left operand is not equal to the right operand . Otherwise FALSE

    • !== is not equal to return TRUE when the left operand is not equal to the right operand or the two types are unequal. Otherwise returns FALSE

<?php $a =1;                           Declares an integer variable $ A value of 1var_dump ($a > 1);              The result of the comparison is bool (false), 1 less than 1var_dump ($a < true);          The result of the comparison is bool (false), and the ture will automatically convert to 1var_dump ($a >= 0.01);         The result of the comparison is bool (true), 1 greater than 0.01var_dump ($a <= "0.10yuan");   The result of comparison is bool (false), "0.10yuan" will automatically turn to 0.10 and then compare var_dump ($a = = 1);            The result of the comparison is bool (true), 1 equals 1var_dump ($a = = "1");          The result of the comparison is bool (true), and "1" is automatically converted to 1 for comparison, equal to var_dump ($a = = = "1");         The result of the comparison is bool (false), although the content is the same, but not the same type of value var_dump ($a = = = 1);           The result of the comparison is bool (true), the content is the same, and the type is the same var_dump ($a <> 1);            The result of the comparison is bool (false), 1 equals 1, so it is false var_dump ($a! = 1);            The result of the comparison is bool (false), Ibid. var_dump ($a! = 0);            The result of the comparison is bool (true), and 1 is not equal to 0var_dump ($a!== "1");         The result of the comparison is bool (true), although the content is the same, but the type is different?>

5. Logical operators

PHP provides 4 logical operators: Logical AND ( and or && ), logical OR ( or or || ), logical non-( not or ! ), and XOR ( xor ).

    • Logic and: The relationship between the logic and the expression "and", both sides of the expressions must be TRUE , the result can be true, otherwise the entire expression is false.

    • Logical OR: A relationship that is logical or represents "or", where the expression on either side is one TRUE , the result is true, or the entire expression is false.

    • Logical non: logical non-representation of the "inverse" relationship, if the expression is TRUE , the result becomes FALSE , if the expression is FALSE , the result is TRUE .

    • Logical XOR: A logical XOR or an expression on both sides of an operation is not at the same time, that is TRUE TRUE , it must be one side of true and the other FALSE . Both sides of the expression at the same time, whether it is TRUE or both FALSE , the result is FALSE .

$username = "Apple";                Save the user name Apple in the variable $username $password = "123456";               Save the user password 123456 in the variable $password, "apple@163.com";       Save User e-mail apple@163.com in variable  $phone = "400-7654321";             Save user phone 400-7654321 in variable $phone    //Use a "logical and" operator, and a comparison operator together to determine if ($username = = "Apple" && $password = = "123456") {           echo "username and password entered correctly";}    Use a multiple "logical or" operator, together with a comparison operator, to determine if ($username = = "" "| | $password = = "" | | $email = = "" | | $phone = = "") {           echo "All values cannot be empty";}    Multiple different logical operators are mixed and used together with the return Boolean function as a condition to determine if (Isset ($email) &&!empty ($email)) | | (Isset ($phone) &&!empty ($phone))) {           echo "at least one contact method";}

6. Ternary operators

?:can provide a simple logical judgment, in PHP, the ternary operator in this one root only one. is equivalent to a conditional statement if...else... . Its syntax format is as follows: (exprl)? (expr2):(expr3) . The meaning is that exprl when evaluated, it TRUE executes ? and : expr2 gets its value, exprl FALSE when evaluated, after it executes : expr3 and gets its value.

<?php     $money =100;    $result = $money >200? " "Good money": "no money spent";        Output no money spent the    echo $result; >
Related Article

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.